#include using namespace std; template ostream& operator<<(ostream& out, const vector &cont) { out << "["; for(const auto &x: cont) out << x<< ", "; out << "]"; return out; } #define dmp(x) cerr << #x << " = " << x << endl #define dmpn(x) cerr << #x << " = " << x <<"; " #define ff first #define ss second #define all(x) begin(x), end(x) #define sz(x) (int) (x).size() #define int long long typedef long long ll; typedef pair pii; typedef long double ld; int components = 0; set visited; vector> points; map> row, col; void dfs(pii i){ if(visited.find(i) != visited.end()) return; visited.insert(i); for(int x:row[i.second]){ dfs({x, i.second}); } for(int y:col[i.first]){ dfs({i.first, y}); } } void solve() { int n; cin >> n; for(int i=0; i> x >> y; row[y].push_back(x); col[x].push_back(y); points.push_back({x,y}); } for(auto &i:points){ if(visited.find(i) == visited.end()){ components++; dfs(i); } } cout << components-1 << '\n'; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); solve(); return 0; }