#include <bits/stdc++.h>
using namespace std;
int n, x[111], y[111];
bool mark[111];
void dfs(int i) {
  mark[i] = false;
  for (int j = 1; j <= n; j++)
    if (mark[j] && (x[j] == x[i] || y[j] == y[i])) dfs(j);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]);
  fill(mark + 1, mark + n + 1, true);
  int ret = -1;
  for (int i = 1; i <= n; i++)
    if (mark[i]) {
      dfs(i);
      ret++;
    }
  printf("%d", ret);
  return 0;
}
