from math import sqrt,sin,cos,tan,ceil,radians,floor,gcd,exp,log,log10,log2,factorial,fsum from heapq import heapify, heappop, heappush from bisect import bisect_left, bisect_right from copy import deepcopy import copy import random from collections import deque,Counter,defaultdict from itertools import permutations,combinations from decimal import Decimal,ROUND_HALF_UP #tmp = Decimal(mid).quantize(Decimal('0'), rounding=ROUND_HALF_UP) from functools import lru_cache, reduce #@lru_cache(maxsize=None) from operator import add,sub,mul,xor,and_,or_,itemgetter INF = 10**18 mod1 = 10**9+7 mod2 = 998244353 #DecimalならPython #再帰ならPython!!!!!!!!!!!!!!!!!!!!!!!!!! class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n #親だけ self.edge = [0]*n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): x = self.find(x) y = self.find(y) if self.parents[x] > self.parents[y]: x, y = y, x self.edge[x] += 1 if x == y: return self.parents[x] += self.parents[y] self.edge[x] += self.edge[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def 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.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) ''' グラフっぽく考えられそう 最初と終わりさえ決めれば間は関係ない 全部を通って終わりまでたどりつけるか?という問題 sccして1個なら全部行ける 1個じゃなくて、パスがあったらその組はいける 入次数0から始める そこから全部通れるか サイクルはSCCで対処可能 ''' N = int(input()) lim = 2*(10**5)+1 lef = [[] for _ in range(lim)] rig = [[] for _ in range(lim)] mem = set() hw = [] for i in range(N): h,w = map(int, input().split()) hw.append((h,w)) lef[h].append((w,i)) mem.add(h) mem.add(w) M = len(mem) uf = UnionFind(N) memo1 = [0]*(lim) memo2 = [0]*(lim) i = 0 edge = [] for h,w in hw: for l,j in lef[w]: memo1[j] += 1 memo2[i] += 1 uf.union(i,j) edge.append((h,l)) i += 1 if uf.group_count() != 1: print(0) exit() ls = [] for i in range(N): ls.append(memo1[i]-memo2[i]) ls.sort() if ls[0] == ls[-1] == 0: print(M) elif ls[0] == -ls[-1] and ls[1] == ls[-2] == 0: print(1) else: print(0)