#include using namespace std; using ll = long long; struct UnionFind { UnionFind(int n) : p(n, -1) {} int root(int u) { return p[u] < 0 ? u : p[u] = root(p[u]); } int size(int u) { return -p[root(u)]; } bool same(int u, int v) { return root(u) == root(v); } bool unite(int u, int v) { u = root(u); v = root(v); if (u == v) return false; if (p[u] > p[v]) swap(u, v); p[u] += p[v]; p[v] = u; return true; } vector p; }; int main() { int n; cin >> n; UnionFind uf((int)2e5 + 1); unordered_map mp; for (int i = 0; i < n; i++) { int h, w; cin >> h >> w; uf.unite(h, w); mp[h]--; mp[w]++; } if (uf.size(mp.begin()->first) != mp.size()) { cout << 0 << endl; exit(0); } int x = 0; for (const auto &[i, d] : mp) { x += abs(d); } if (x == 0) { cout << mp.size() << endl; } else if (x == 2) { cout << 1 << endl; } else { cout << 0 << endl; } return 0; }