#include <bits/stdc++.h>
int a[110], b[110];
int use[110];
int n;
void dfs(int x) {
  use[x] = 1;
  for (int j = 0; j < n; ++j)
    if (!use[j] && (a[x] == a[j] || b[x] == b[j])) dfs(j);
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) scanf("%d %d", a + i, b + i);
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (!use[i]) {
      ++ans;
      dfs(i);
    }
  printf("%d\n", ans - 1);
}
