#include <bits/stdc++.h>
using namespace std;
long long const MOD = 1000000007;
const int Mx = 2e5 + 10;
vector<pair<long long, long long> > v;
vector<long long> adj[1000];
bool vis[1000];
void DFS(long long s) {
  for (auto x : adj[s]) {
    if (!vis[x]) {
      vis[x] = true;
      DFS(x);
    }
  }
}
int main() {
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    v.push_back({a, b});
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = i + 1; j < n; j++) {
      if (v[i].first == v[j].first || v[i].second == v[j].second) {
        adj[i].push_back(j);
        adj[j].push_back(i);
      }
    }
  }
  long long cnt = -1;
  for (long long i = 0; i < n; i++) {
    if (!vis[i]) {
      vis[i] = true;
      DFS(i);
      cnt++;
    }
  }
  cout << cnt << endl;
}
