class UnionFind: def __init__(self, n): self.parent = list(range(n)) #親ノード self.size = [1]*n #グループの要素数 def root(self, x): #root(x): xの根ノードを返す. while self.parent[x] != x: self.parent[x] = self.parent[self.parent[x]] x = self.parent[x] return x def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる x, y = self.root(x), self.root(y) if x == y: return False if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように self.size[x] += self.size[y] #xの要素数を更新 self.parent[y] = x #yをxにつなぐ return True def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue return self.root(x) == self.root(y) def getsize(self,x): #size(x): xのいるグループの要素数を返す return self.size[self.root(x)] n = int(input()) N = 2*10**5+1 ind = [0]*N out = [0]*N UF = UnionFind(N) for _ in range(n): h,w = map(int,input().split()) ind[h] += 1 out[w] += 1 UF.merge(h,w) r = UF.root(h) nonzero = [i for i in range(N) if ind[i] != out[i]] if len(nonzero) == 0: ans = sum(ind[i]+out[i] > 0 for i in range(N)) elif len(nonzero) == 2: i,j = nonzero if abs(ind[i] - out[i]) == 1: ans = 1 else: ans = 0 else: ans = 0 for i in range(N): if (ind[i] or out[i]) and UF.root(i) != r: ans = 0 print(ans)