from sys import stdin n, *indata = map(int, stdin.read().split()) offset = 0 g = [0 for i in range((10**5)*2+1)] ginv = [0 for i in range((10**5)*2+1)] for i in range(n): s, t = indata[offset + 2*i],indata[offset + 2*i+1] g[s] += 1 ginv[t] += 1 plusused = False minusused = False for i in range(1,(10**5)*2+1): if abs(g[i] - ginv[i]) >= 2: print("{}".format(0)) exit() elif g[i] - ginv[i] == 1: if plusused: print("{}".format(0)) exit() else: plusused = True elif g[i] - ginv[i] == 1: if minusused: print("{}".format(0)) exit() else: minusused = True if plusused: print("{}".format(1)) exit() ans = 0 for i in range(1,(10**5)*2+1): if g[i]: ans += 1 print("{}".format(ans))