n = int(input()) P_C = {} C_P = {} v = set() z = {} for _ in range(n): x, y = map(int, input().split()) if x in v: z[x] += 1 else: v.add(x) z[x] = 1 if y in v: z[y] -= 1 else: v.add(y) z[y] = -1 x_P = C_P.get(x, 0) y_P = C_P.get(y, 0) if x_P == 0 and y_P == 0: C_P[x] = x C_P[y] = x P_C[x] = [x, y] elif x_P == 0: C_P[x] = y_P P_C[y_P].append(x) elif y_P == 0: C_P[y] = x_P P_C[x_P].append(y) elif x_P != y_P: P = P_C[y_P].copy() for i in P: C_P[i] = x_P P_C[x_P] += P del P_C[y_P] count_p1 = 0 count_0 = 0 count_m1 = 0 for i in v: if z[i] == 0: count_0 += 1 elif z[i] == 1: count_p1 += 1 elif z[i] == -1: count_m1 += 1 if len(P_C) ==1 and count_m1 == 1 and count_p1 == 1 and count_0 == len(v) - 2: print(1) elif len(P_C) ==1 and count_0 == len(v): print(len(v)) else: print(0)