class Solution:
def unhappyFriends(self, n: int, preferences: List[List[int]], pairs: List[List[int]]) -> int:
res = set()
pair_dit = {}
for x,y in pairs:
pair_dit[x] = y
pair_dit[y] = x
for x in range(n):
y = pair_dit[x]
for u in preferences[x][:preferences[x].index(y)]:
v = pair_dit[u]
if preferences[u].index(x) < preferences[u].index(v):
res.add(x)
return len(res)