#include <bits/stdc++.h>
#define int long long
using namespace std;
struct info {
int x, y;
};
int n;
vector<info> snow(1111);
bool visited[1111];
void BFS(int s) {
queue<int> q;
q.push(s);
visited[s] = true;
while (!q.empty()) {
int u = q.front();
q.pop();
for (int v = 1; v <= n; v += 1) {
if (!visited[v] && (snow[u].x == snow[v].x || snow[u].y == snow[v].y)) {
visited[v] = true;
q.push(v);
}
}
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
int res = 0;
for (int i = 1; i <= n; i += 1) {
cin >> snow[i].x >> snow[i].y;
}
for (int i = 1; i <= n; i += 1) {
if (!visited[i]) {
BFS(i);
res += 1;
}
}
cout << res - 1;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgaW50IGxvbmcgbG9uZwoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBpbmZvIHsKICAgIGludCB4LCB5Owp9OwoKaW50IG47CnZlY3RvcjxpbmZvPiBzbm93KDExMTEpOwpib29sIHZpc2l0ZWRbMTExMV07Cgp2b2lkIEJGUyhpbnQgcykgewogICAgcXVldWU8aW50PiBxOwogICAgcS5wdXNoKHMpOwogICAgdmlzaXRlZFtzXSA9IHRydWU7CgogICAgd2hpbGUgKCFxLmVtcHR5KCkpIHsKICAgICAgICBpbnQgdSA9IHEuZnJvbnQoKTsKICAgICAgICBxLnBvcCgpOwoKICAgICAgICBmb3IgKGludCB2ID0gMTsgdiA8PSBuOyB2ICs9IDEpIHsKICAgICAgICAgICAgaWYgKCF2aXNpdGVkW3ZdICYmIChzbm93W3VdLnggPT0gc25vd1t2XS54IHx8IHNub3dbdV0ueSA9PSBzbm93W3ZdLnkpKSB7CiAgICAgICAgICAgICAgICB2aXNpdGVkW3ZdID0gdHJ1ZTsKICAgICAgICAgICAgICAgIHEucHVzaCh2KTsKICAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9Cn0KCmludDMyX3QgbWFpbigpIHsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShOVUxMKTsKCiAgICBjaW4gPj4gbjsKCiAgICBpbnQgcmVzID0gMDsKCiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpICs9IDEpIHsKICAgICAgICBjaW4gPj4gc25vd1tpXS54ID4+IHNub3dbaV0ueTsKICAgIH0KCiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpICs9IDEpIHsKICAgICAgICBpZiAoIXZpc2l0ZWRbaV0pIHsKICAgICAgICAgICAgQkZTKGkpOwogICAgICAgICAgICByZXMgKz0gMTsKICAgICAgICB9CiAgICB9CgogICAgY291dCA8PCByZXMgLSAxOwoKICAgIHJldHVybiAwOwp9Cg==