n = int(input()) max_ = 2 * 10 ** 5 edges = [[] for _ in range(max_)] in_cnt = [0] * max_ out_cnt = [0] * max_ for _ in range(n): h, w = map(int, input().split()) h -= 1 w -= 1 edges[h].append(w) in_cnt[h] += 1 out_cnt[w] += 1 s = -1 g = -1 for j, (i, o) in enumerate(zip(in_cnt, out_cnt)): if i == o: continue if i == o + 1: if s == -1: s = j else: print(0) exit() elif i + 1 == o: if g == -1: g = j else: print(0) exit() else: print(0) exit() stack = [s] used = [False] * n used[s] = True while stack: pos = stack.pop() for npos in edges[pos]: if used[npos]: continue used[npos] = True for i in range(max_): if not used[i] and in_cnt[i] != 0: print(0) exit() if s == g == -1: stack = [h] used = [False] * n used[h] = True while stack: pos = stack.pop() for npos in edges[pos]: if used[npos]: continue used[npos] = True for i in range(max_): if not used[i] and in_cnt[i] != 0: print(0) exit() ans = 0 for e in edges: ans += len(set(e)) print(ans) exit() cnt = 0 used = [False] * n stack = [s] used[s] = True while stack: pos = stack.pop() for npos in edges[pos]: if used[npos]: continue if npos == g: cnt += 1 continue used[npos] = True stack.append(npos) l = 0 for e in edges: if g in e: l += 1 if cnt == 1: print(max(1, l - 1)) else: print(l)