import sys def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N = I() M = 2*10**5 in_deg = [0]*(M+1) out_deg = [0]*(M+1) for _ in range(N): H,W = MI() out_deg[H] += 1 in_deg[W] += 1 a,b,c = 0,0,0 for i in range(1,M+1): if in_deg[i] == out_deg[i]: if in_deg[i]: a += 1 elif in_deg[i] == out_deg[i]+1: b += 1 elif in_deg[i]+1 == out_deg[i]: c += 1 else: print(0) exit() if b > 1 or c > 1 or b != c: print(0) exit() if b: print(1) else: print(a)