#include <bits/stdc++.h>
using namespace std;
const int INF = 0x7fffffff;
struct Node {
  int x, y;
} node[110];
int num[110];
int find(int ii) {
  int a;
  if (num[ii] == ii)
    return ii;
  else {
    a = find(num[ii]);
    num[ii] = a;
    return a;
  }
}
int main() {
  int t, t1;
  int i, j, k, a, b;
  int m, n;
  while (scanf("%d", &n) != EOF) {
    for (i = 1; i <= n; i++) {
      scanf("%d%d", &node[i].x, &node[i].y);
      num[i] = i;
    }
    for (i = 2; i <= n; i++) {
      for (j = 1; j < i; j++) {
        if (node[i].x == node[j].x || node[i].y == node[j].y) {
          a = find(i);
          b = find(j);
          num[a] = b;
        }
      }
    }
    k = 0;
    for (i = 1; i <= n; i++) {
      if (num[i] == i) k++;
    }
    printf("%d\n", k - 1);
  }
  return 0;
}
