#include <bits/stdc++.h>
using namespace std;
int enter[101] = {0};
int x[101], y[101];
int n;
void dfs(int ice) {
  enter[ice] = 1;
  for (int i = 1; i <= n; i++) {
    if (i != ice && enter[i] == 0 && (x[i] == x[ice] || y[i] == y[ice])) {
      dfs(i);
    }
  }
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x[i], &y[i]);
  }
  int count = 0;
  for (int i = 0; i < n; i++) {
    if (enter[i] == 0) {
      dfs(i);
      count++;
    }
  }
  printf("%d\n", count - 1);
}
