#include <bits/stdc++.h>
using namespace std;
struct Node {
  int x, y;
};
Node d[1010];
int father[1010], rnk[1010];
int nn;
int Find(int r) {
  return father[r] = (father[r] == r) ? father[r] : Find(father[r]);
}
void Merge(int x, int y) {
  int fx = Find(x), fy = Find(y);
  if (fx != fy) {
    if (rnk[fx] >= rnk[fy]) {
      father[fy] = fx;
      if (rnk[fx] == rnk[fy]) rnk[fx] += rnk[fy];
    } else {
      father[fx] = fy;
    }
  }
}
int main() {
  int i, j;
  while (~scanf("%d", &nn)) {
    for (i = 1; i <= nn; i++) {
      father[i] = i;
      rnk[i] = 1;
    }
    for (i = 1; i <= nn; i++) {
      scanf("%d%d", &d[i].x, &d[i].y);
      for (j = 1; j < i; j++) {
        if (d[i].x == d[j].x || d[i].y == d[j].y) {
          Merge(i, j);
        }
      }
    }
    int ans = 0;
    for (i = 1; i <= nn; i++) {
      int r = Find(i);
      if (r == i) ans++;
    }
    printf("%d\n", ans - 1);
  }
  return 0;
}
