import sys sys.setrecursionlimit(500000) def scc(edge): n = len(edge) redge = [[] for i in range(n)] for u in range(n): for v in edge[u]: redge[v].append(u) seen = [0] * n order = [] group = [0] * n def dfs(u): seen[u] = 1 for v in edge[u]: if seen[v] == 0: dfs(v) order.append(u) def rdfs(u, label): seen[u] = 1 group[u] = label for v in redge[u]: if seen[v] == 0: rdfs(v, label) for u in range(n): if seen[u] == 0: dfs(u) seen = [0] * n label = 0 for u in reversed(order): if seen[u] == 0: rdfs(u, label) label += 1 return group from collections import Counter n = int(input()) m = 10 e = [[] for _ in range(m)] ls = [list(map(lambda x: int(x)-1, input().split())) for _ in range(n)] ind = [0] * m for h, w in ls: e[h].append(w) ind[w] += 1 g = scc(e) cnt = [0, 0] for i in range(m): if abs(len(e[i]) - ind[i]): if abs(len(e[i]) - ind[i]) > 1: print(0) exit() if len(e[i]) > ind[i]: cnt[0] += 1 else: cnt[1] += 1 if cnt[0] != cnt[1] or max(cnt) > 1: print(0) exit() elif cnt[0]: print(1) else: print(max(Counter(g).values()))