#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
} p[1005];
int bin[1005];
int findx(int x) {
  if (bin[x] == x) return x;
  return bin[x] = findx(bin[x]);
}
void merge(int x, int y) {
  int fx, fy;
  fx = findx(x);
  fy = findx(y);
  if (fx != fy) bin[fx] = fy;
}
int main() {
  int i, j, cas, n;
  while (scanf("%d", &n) != EOF) {
    for (i = 1; i <= n; i++) {
      bin[i] = i;
    }
    int xx = 0;
    for (i = 1; i <= n; i++) {
      scanf("%d%d", &p[i].x, &p[i].y);
      for (j = 1; j < i; j++) {
        if (p[i].x == p[j].x || p[i].y == p[j].y) {
          merge(i, j);
        }
      }
    }
    for (i = 1; i <= n; i++) {
      if (bin[i] == i) xx++;
    }
    printf("%d\n", xx - 1);
  }
  return 0;
}
