#include <bits/stdc++.h>
using namespace std;
int father[1001];
int findfather(int x) {
  if (father[x] == x)
    return x;
  else {
    father[x] = findfather(father[x]);
    return father[x];
  }
}
void merge(int a, int b) {
  int fa = findfather(a);
  int fb = findfather(b);
  if (fa != fb) {
    father[fb] = fa;
  }
}
int main() {
  int n;
  while (cin >> n && n) {
    int x[n + 1], y[n + 1], t = 0;
    for (int i = 1; i <= n; i++) father[i] = i;
    for (int i = 1; i <= n; i++) {
      cin >> x[i] >> y[i];
    }
    for (int i = 1; i < n; i++) {
      for (int j = i + 1; j <= n; j++) {
        if (x[i] == x[j] || y[i] == y[j]) {
          merge(i, j);
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      if (father[i] == i) t++;
    }
    cout << t - 1 << endl;
  }
  return 0;
}
