#include "bits/stdc++.h" using namespace std; #define ll long long bool add_node(set& xv, set& yv, pair x) { bool r = true; if (xv.find(x.first) != xv.end() || yv.find(x.second) != yv.end()) r = false; xv.insert(x.first); yv.insert(x.second); return r; } void solve() { int n; cin >> n; multiset> e; for (int i = 0; i < n; i++) { ll a, b; cin >> a >> b; e.insert({a, b}); } set xv, yv; int g_c = 0; for (pair x : e) { if (add_node(xv, yv, x)) g_c++; } cout << g_c - 1 << '\n'; } int main() { solve(); }