#include <bits/stdc++.h>
int n;
int y[101];
int x[101];
int used[101];
int label[101];
int sqr(int a) { return a * a; }
int rec(int a) {
  for (int j = 0; j < n; j++) {
    if (j != a && (x[a] == x[j] || y[j] == y[a]) && used[j] == 0) {
      used[j] = 1;
      label[j] = label[a];
      rec(j);
    }
  }
  return 0;
}
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++) used[i] = 0;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (used[i] == 0) {
      label[i] = cnt;
      used[i] = 1;
      rec(i);
      cnt++;
    }
  }
  printf("%d\n", cnt - 1);
  return 0;
}
