#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; void solve() { int n; cin >> n; set lines, rows; int components = 0; for(int i=0; i> x >> y; if(lines.find(x) == lines.end() && rows.find(y) == rows.end()) components++; lines.insert(x); rows.insert(y); } cout << components-1 << '\n'; } int32_t main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); solve(); return 0; }