#include <bits/stdc++.h>
using namespace std;
int n, x[1111], y[1111], p[1111];
int fi(int x) { return p[x] == x ? x : p[x] = fi(p[x]); }
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    p[i] = i;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (x[i] == x[j] || y[i] == y[j]) {
        int px = fi(i);
        int py = fi(j);
        if (px != py) {
          p[px] = py;
          ans++;
        }
      }
    }
  }
  cout << n - 1 - ans << endl;
  return 0;
}
