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