#include <bits/stdc++.h>
bool a[105][105], used[105];
int x[105], y[105], n, i, j, cnt = 0;
void dfs(int node) {
  used[node] = 1;
  for (int i = 0; i < n; i++)
    if (!used[i] && a[node][i]) dfs(i);
}
int main() {
  scanf("%d", &n);
  for (i = 0; i < n; i++) scanf("%d %d", &x[i], &y[i]);
  for (i = 0; i < n; i++)
    for (j = i + 1; j < n; j++)
      if (x[i] == x[j] || y[i] == y[j]) a[i][j] = a[j][i] = 1;
  for (i = 0; i < n; i++)
    if (!used[i]) {
      dfs(i);
      cnt++;
    }
  printf("%d\n", cnt - 1);
  return 0;
}
