n = int(input()) count_h = [0] * 200000 count_w = [0] * 200000 loop = [0] * 200000 for i in range(n): h, w = map(int, input().split()) h -= 1 w -= 1 if h == w: loop[h] += 1 else: count_h[h] += 1 count_w[w] += 1 ans = 0 diff = [] for i in range(200000): if abs(count_h[i] - count_w[i]) > 1: print(0) exit() elif count_h[i] == 0 and count_w[i] == 0 and loop[i] != 0 and loop[i] != n: print(0) exit() elif count_h[i] != count_w[i]: diff.append(i) elif count_h[i] or loop[i]: ans += 1 if len(diff) > 2: print(0) exit() if diff: print(1) else: print(ans)