#include using namespace std; using ll = int64_t; using ull = uint64_t; struct P { int x, y; }; vector

pts; map> xtop, ytop; void dfs(int curr, vector &seen) { if (seen[curr]) return; seen[curr] = true; auto it = xtop.find(pts[curr].x); if (it != xtop.end()) { vector neighs = it->second; xtop.erase(it); for (int n : neighs) { dfs(n, seen); } } it = ytop.find(pts[curr].y); if (it != ytop.end()) { vector neighs = it->second; ytop.erase(it); for (int n : neighs) { dfs(n, seen); } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); // cout << setprecision(20); int n; cin >> n; pts.reserve(n); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; pts.push_back({x, y}); xtop[x].push_back(i); ytop[y].push_back(i); } vector seen(n, false); int ret = -1; for (int i = 0; i < n; ++i) { if (seen[i]) continue; ++ret; dfs(i, seen); } cout << ret << endl; return 0; }