from collections import Counter class UnionFind: __slots__ = ["n", "parent", "height", "size"] def __init__(self, n): self.n = n self.parent = [i for i in range(n)] self.height = [1] * n self.size = [1] * n def find(self, x): if self.parent[x] == x: return x else: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.height[x] < self.height[y]: self.parent[x] = y self.size[y] += self.size[x] else: self.parent[y] = x self.size[x] += self.size[y] if self.height[x] == self.height[y]: self.height[x] += 1 def issame(self, x, y): return self.find(x) == self.find(y) def group_size(self, x): return self.size[self.find(x)] def group_members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parent) if i == x] def group_count(self): return len(self.roots()) N = int(input()) MAX = 2 * 10**5 deg = [0] * MAX st = set() uf = UnionFind(MAX) for _ in range(N): H, W = (int(x) - 1 for x in input().split()) deg[H] += 1 deg[W] -= 1 uf.unite(H, W) st.add(H) st.add(W) # 連結か if len(set(uf.find(x) for x in st)) == 1: ct = Counter(deg) if ct[0] == MAX: print(len(st)) elif ct[0] == MAX - 2 and ct[-1] == ct[1] == 1: print(1) else: print(0) else: print(0)