#include <bits/stdc++.h>
int n;
struct data {
  int x, y;
} a[105];
int p[105], rst;
int find(int x) { return x == p[x] ? x : find(p[x]); }
bool ck[105];
int main() {
  int i;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) scanf("%d%d", &a[i].x, &a[i].y), p[i] = i;
  for (i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (i != j && (a[i].x == a[j].x || a[i].y == a[j].y) &&
          find(i) != find(j))
        p[find(i)] = find(j);
    }
  for (i = 1; i <= n; i++) ck[find(i)] = 1;
  for (i = 1; i <= n; i++)
    if (ck[i]) rst++;
  printf("%d", rst - 1);
}
