#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
  bool operator<(const node &a) const { return x < a.x; }
} arr[200];
struct table {
  bool xt[200], yt[200];
} ft[200];
int father[200];
bool check[200];
vector<int> xtab, ytab;
int getf(int id) {
  if (id == father[id]) return id;
  int nf = getf(father[id]);
  father[id] = nf;
  for (int i = 0; i < 150; i++) {
    ft[nf].xt[i] = ft[nf].xt[i] || ft[id].xt[i];
    ft[nf].yt[i] = ft[nf].yt[i] || ft[id].yt[i];
  }
  return nf;
}
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    xtab = vector<int>();
    ytab = vector<int>();
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &arr[i].x, &arr[i].y);
      xtab.push_back(arr[i].x);
      ytab.push_back(arr[i].y);
    }
    vector<int>::iterator it;
    sort(xtab.begin(), xtab.end());
    sort(ytab.begin(), ytab.end());
    it = unique(xtab.begin(), xtab.end());
    xtab.resize(it - xtab.begin());
    it = unique(ytab.begin(), ytab.end());
    ytab.resize(it - ytab.begin());
    for (int i = 0; i < n; i++) {
      arr[i].x = find(xtab.begin(), xtab.end(), arr[i].x) - xtab.begin();
      arr[i].y = find(ytab.begin(), ytab.end(), arr[i].y) - ytab.begin();
    }
    sort(arr, arr + n);
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j < 150; j++) {
        ft[i].xt[j] = false;
        ft[i].yt[j] = false;
      }
    }
    for (int i = 0; i < n; i++) {
      father[i] = i;
      ft[i].xt[arr[i].x] = true;
      ft[i].yt[arr[i].y] = true;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        int f = getf(i);
        int jf = getf(j);
        if (jf != f && (ft[f].xt[arr[j].x] || ft[f].yt[arr[j].y])) {
          ft[f].xt[arr[j].x] = true;
          ft[f].yt[arr[j].y] = true;
          father[jf] = f;
          for (int k = 0; k < 150; k++) {
            ft[f].xt[k] = ft[f].xt[k] || ft[jf].xt[k];
            ft[f].yt[k] = ft[f].yt[k] || ft[jf].yt[k];
          }
        }
      }
    }
    for (int i = 0; i < n; i++) check[i] = false;
    for (int i = 0; i < n; i++) check[father[i]] = true;
    int cnt = 0;
    for (int i = 0; i < n; i++)
      if (check[i]) cnt++;
    printf("%d\n", cnt - 1);
  }
  return 0;
}
