#include <bits/stdc++.h>
struct Po {
  int x, y;
} a[105];
int n, vis[105];
void dfs(int i) {
  vis[i] = 1;
  int j;
  for (j = 0; j < n; j++)
    if (vis[j] == 0 && (a[i].x == a[j].x || a[i].y == a[j].y)) dfs(j);
}
int main() {
  int i, j, k, num;
  while (scanf("%d", &n) != EOF) {
    for (i = 0; i < n; i++) scanf("%d %d", &a[i].x, &a[i].y);
    memset(vis, 0, sizeof(vis));
    num = 0;
    for (i = 0; i < n; i++) {
      if (vis[i] == 0) {
        num++;
        dfs(i);
      }
    }
    printf("%d\n", num - 1);
  }
  return 0;
}
