#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<pair<int, int> > rank;
  int root[1002], ran[1002];
  int j, i, x, y, n, k;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    rank.push_back(make_pair(x, y));
    root[i] = i;
    ran[i] = 0;
  }
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (rank[i].first == rank[j].first || rank[i].second == rank[j].second) {
        if (root[i] == root[j]) continue;
        if (ran[root[i]] > ran[root[j]]) {
          for (k = 0; k < n; k++)
            if (root[k] == root[j]) root[k] = root[i];
        } else if (ran[root[i]] < ran[root[j]]) {
          for (k = 0; k < n; k++)
            if (root[k] == root[i]) root[k] = root[j];
        } else {
          for (k = 0; k < n; k++)
            if (root[k] == root[j]) root[k] = root[i];
          ran[root[i]]++;
        }
      }
    }
  }
  sort(root, root + n);
  k = 0;
  for (i = 0; i < n - 1; i++) {
    if (root[i] != root[i + 1]) k++;
  }
  cout << k;
  return 0;
}
