// #pragma GCC optimize ("Ofast") // #pragma GCC optimize ("unroll-loops") // #pragma GCC target ("avx,avx2,fma") #include using std::cin, std::cout, std::cerr; using ll = long long; int main() { std::ios::sync_with_stdio(false); int n; cin >> n; int m = 2e5; std::vector p(m + 1), sz(m + 1, 1), ecnt(m + 1), deg(m + 1); std::iota(p.begin(), p.end(), 0); std::function root = [&](int x) { if(p[x] == x) return x; return p[x] = root(p[x]); }; for(int i = 1; i <= n; i ++) { int x, y; cin >> x >> y; deg[x] --; deg[y] ++; int rx = root(x), ry = root(y); if(rx != ry) { ecnt[rx] ++; sz[rx] += sz[ry]; p[ry] = rx; } else { ecnt[rx] ++; } } int cnt = 0, id; for(int i = 1; i <= m; i ++) if(p[i] == i && ecnt[i] > 0) { cnt ++; id = i; } if(cnt == 1) { if(std::count(deg.begin(), deg.end(), 0) == m + 1) { cout << sz[id] << '\n'; return 0; } else if(std::count(deg.begin(), deg.end(), 0) == m + 1 - 2 && std::count(deg.begin(), deg.end(), 1) == 1 && std::count(deg.begin(), deg.end(), -1) == 1) { cout << 1 << '\n'; return 0; } } cout << 0 << '\n'; }