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

pts; unordered_map> xtop, ytop; void dfs(int curr, vector &seen) { if (seen[curr]) return; seen[curr] = true; if (xtop.find(pts[curr].x) != xtop.end()) { for (int n : xtop[pts[curr].x]) { dfs(n, seen); } xtop.erase(pts[curr].x); } if (ytop.find(pts[curr].y) != ytop.end()) { for (int n : ytop[pts[curr].y]) { dfs(n, seen); } ytop.erase(pts[curr].y); } } 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; }