#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;
}
