MATCH (country:Country {name: 'Spain'})
with country
MATCH (a:Person)-[:IS_LOCATED_IN]->(:City)-[:IS_PART_OF]->(country)
with collect(distinct a) as aPrime
with aPrime, aPrime as bPrime
unwind aPrime as ap
with ap,bPrime
unwind bPrime as bp
MATCH (ap)-[:KNOWS]-(bp)
where ap.id < bp.id
with ap, bp
match (bp)-[:KNOWS]-(c) where c.id<ap.id
and (c)-[:IS_LOCATED_IN]->(:City)-[:IS_PART_OF]->(:Country {name: 'Spain'})
and (c)-[:KNOWS]-(ap)
RETURN count(*) AS count
MATCH (country:Country {name: 'Spain'})
with country
MATCH (a:Person)-[:IS_LOCATED_IN]->(:City)-[:IS_PART_OF]->(country)
with collect(distinct a) as aPrime
with aPrime, aPrime as bPrime
unwind aPrime as ap
with ap,bPrime
unwind bPrime as bp
MATCH (ap)-[:KNOWS]-(bp)
where ap.id < bp.id
with ap, bp
match (bp)-[:KNOWS]-(c) where c.id<ap.id
and (c)-[:IS_LOCATED_IN]->(:City)-[:IS_PART_OF]->(:Country {name: 'Spain'})
and (c)-[:KNOWS]-(ap)
RETURN count(*) AS count