#include using namespace std; using ll = long long; using pll = pair; int main() { std::ios::sync_with_stdio(false); ll N; cin >> N; set x; set y; vector points; set seen; for (int i = 0; i < N; i++) { ll x_0, y_0; cin >> x_0 >> y_0; pll point = pair(x_0, y_0); pll r_point = pair(y_0, x_0); points.push_back(point); x.insert(point); y.insert(r_point); } ll count = 0; for (int i = 0; i < N; i++) { pll point = points[i]; if (seen.find(point) == seen.end()) { count++; deque q; q.push_back(point); while(!q.empty()) { pll p = q.front(); q.pop_front(); if (seen.find(p) != seen.end()) { continue; } seen.insert(p); pll r_p = pair(p.second, p.first); pll c = *(++x.find(p)); pll d = *(--x.find(p)); if (c.first == p.first) q.push_back(*(++x.find(p))); if (d.first == p.first) q.push_back(*(--x.find(p))); pll a = *(++y.find(r_p)); pll b = *(--y.find(r_p)); if (a.first == p.second) q.push_back(pair(a.second, a.first)); if (b.first == p.second) q.push_back(pair(b.second, b.first)); } } } std::cout << count - 1 << endl; return 0; }