MAX = 2 * 10 ** 5 n = int(input()) g = [[] for _ in range(MAX + 1)] inv = [0] * (MAX + 1) out = [0] * (MAX + 1) s = set() for i in range(n): h, w = map(int, input().split()) s.add(h) s.add(w) g[h].append(w) inv[w] += 1 out[h] += 1 f1 = 0 f2 = 0 for i in range(MAX + 1): if inv[i] + 1 == out[i]: f1 += 1 elif out[i] + 1 == inv[i]: f2 += 1 elif inv[i] == out[i]: continue else: print(0) exit(0) if f1 == f2 == 0: print(len(s)) elif f1 == f2 == 1: print(1) else: print(0)