#include <bits/stdc++.h>
using namespace std;
long long c, a[10000], b[10000], d, e, f, i, j, k, n, x, color[1000];
void dfs(long long k) {
  for (long long i = 0; i < n; i++) {
    if (a[i] == a[k] || b[i] == b[k])
      if (i != j && color[i] == 0) {
        color[i] = color[k];
        dfs(i);
      }
  }
  return;
}
int main() {
  cin >> n;
  for (x = 0; x < n; x++) cin >> a[x] >> b[x];
  for (i = 0; i < n; i++) {
    if (color[i] == 0) {
      d++;
      color[i] = d;
      dfs(i);
    } else
      dfs(i);
  }
  cout << d - 1 << endl;
  return 0;
}
