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