#include <bits/stdc++.h>
int n;
int x[100], y[100];
int numkomp = 0;
int komp[100];
bool xx[1001], yy[1001];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%d%d", &x[i], &y[i]);
  for (int i = 0; i < n; i++) komp[i] = -1;
  for (int i = 0; i < n; i++)
    if (komp[i] == -1) {
      komp[i] = ++numkomp;
      for (int j = 1; j <= 1000; j++) xx[j] = yy[j] = false;
      xx[x[i]] = true;
      yy[y[i]] = true;
      for (int k = 0; k < n; k++)
        for (int j = 0; j < n; j++)
          if (komp[j] == -1 && (xx[x[j]] || yy[y[j]])) {
            komp[j] = numkomp;
            xx[x[j]] = true;
            yy[y[j]] = true;
          }
    }
  printf("%d\n", numkomp - 1);
  return 0;
}
