#include <bits/stdc++.h>
struct Map {
  int x, y, a;
} m[102];
int merge(int x) {
  while (m[x].a != x) x = m[x].a;
  return x;
}
int main() {
  int n, i, j, cnt;
  while (scanf("%d", &n) != EOF) {
    for (i = 0; i < n; i++) {
      scanf("%d%d", &m[i].x, &m[i].y);
      m[i].a = i;
    }
    for (i = 0; i < n - 1; i++)
      for (j = i + 1; j < n; j++) {
        if (m[i].x == m[j].x || m[i].y == m[j].y) m[merge(i)].a = m[merge(j)].a;
      }
    cnt = 0;
    for (i = 0; i < n; i++)
      if (m[i].a == i) cnt++;
    printf("%d\n", cnt - 1);
  }
  return 0;
}
