import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main { static int n, n2; static List> list; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); n = Integer.parseInt(br.readLine()); n2 = 0; int m = 200000; list = new ArrayList<>(m); for (int i = 0; i < m; i++) { list.add(new HashSet<>()); } int[] in = new int[m]; int[] out = new int[m]; for (int i = 0; i < n; i++) { String[] sa = br.readLine().split(" "); Hen h = new Hen(); h.a = Integer.parseInt(sa[0]) - 1; h.b = Integer.parseInt(sa[1]) - 1; if (h.a != h.b) { list.get(h.a).add(h); out[h.a]++; in[h.b]++; n2++; } } br.close(); int s = -1; int t = -1; boolean even = false; for (int i = 0; i < m; i++) { if (in[i] != out[i]) { if (Math.abs(in[i] - out[i]) > 1) { System.out.println(0); return; } if (in[i] + 1 == out[i]) { if (s == -1) { s = i; } else { System.out.println(0); return; } } else { if (t == -1) { t = i; } else { System.out.println(0); return; } } } } if (s == -1) { for (int i = 0; i < m; i++) { if (out[i] > 0) { s = i; break; } } even = true; } boolean res = dfs(s, new ArrayList<>(n)); if (res) { if (even) { int ans = 0; for (int i = 0; i < m; i++) { if (!list.get(i).isEmpty()) { ans++; } } System.out.println(ans); } else { System.out.println(1); } } else { System.out.println(0); } } static boolean dfs(int x, List his) { if (his.size() == n2) { return true; } Set set = list.get(x); if (!set.isEmpty()) { Hen h = set.iterator().next(); his.add(h); set.remove(h); boolean res = dfs(h.b, his); set.add(h); his.remove(his.size() - 1); if (res) { return true; } } return false; } static class Hen { int a, b; } }