using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var n = NN; var map = NArr(n); var cin = new int[200001]; var cout = new int[200001]; var uf = new UnionFindTree(200001); foreach (var arr in map) if (arr[0] != arr[1]) { ++cin[arr[0]]; ++cout[arr[1]]; uf.Unite(arr[0], arr[1]); } var scount = 0; for (var i = 0; i < cin.Length; ++i) { if (cin[i] != cout[i]) { scount += Math.Max(0, cin[i] - cout[i]); } } var roots = new HashSet(); foreach (var arr in map) roots.Add(uf.GetRoot(arr[0])); if (roots.Count > 1) { WriteLine(0); } else if (scount == 0) { var set = new HashSet(map.Select(mi => mi[0])); WriteLine(set.Count); } else if (scount == 1) { WriteLine(1); } else { WriteLine(0); } } class UnionFindTree { int[] roots; public UnionFindTree(int size) { roots = new int[size]; for (var i = 0; i < size; ++i) roots[i] = -1; } public int GetRoot(int a) { if (roots[a] < 0) return a; return roots[a] = GetRoot(roots[a]); } public bool IsSameTree(int a, int b) { return GetRoot(a) == GetRoot(b); } public bool Unite(int a, int b) { var x = GetRoot(a); var y = GetRoot(b); if (x == y) return false; if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; } roots[x] += roots[y]; roots[y] = x; return true; } public int GetSize(int a) { return -roots[GetRoot(a)]; } } }