#include <bits/stdc++.h>
using namespace std;
int MOD = 1000000007;
int x[104];
int y[104];
int v[104] = {0};
int n;
void dfs(int j) {
  v[j] = true;
  for (int i = 0; i < n; i++) {
    if ((!v[i]) && (x[i] == x[j] || y[i] == y[j])) {
      dfs(i);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (!v[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << ans - 1;
  return 0;
}
