from scipy.sparse.csgraph import connected_components from scipy.sparse import csr_matrix from collections import Counter N = int(input()) MAX = 2 * 10 ** 5 deg = [0] * MAX st = set() frm, to = [], [] for _ in range(N): H, W = (int(x) - 1 for x in input().split()) deg[H] += 1 deg[W] -= 1 frm.append(H) to.append(W) st.add(H) st.add(W) matr = csr_matrix(([1] * N, (frm, to)), shape=(MAX, MAX)) _, labels = connected_components(matr) # 連結か if len(set(labels[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)