#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > vp;
long long n;
long long id[2001];
void initialize() {
  for (int i = 0; i < 2001; ++i) id[i] = i;
}
long long root(long long x) {
  while (id[x] != x) {
    id[x] = id[id[x]];
    x = id[x];
  }
  return x;
}
void union1(long long x, long long y) {
  long long p = root(x);
  long long q = root(y);
  id[p] = id[q];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long ans = 0, x, y;
  initialize();
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    vp.push_back(make_pair(x, y));
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if ((vp[i].first == vp[j].first || vp[i].second == vp[j].second) &&
          (root(i) != root(j)))
        union1(i, j);
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == id[i]) ans++;
  }
  cout << ans - 1 << endl;
  return 0;
}
