#include <bits/stdc++.h>
using namespace std;
const int MAXN = 101;
int N;
pair<int, int> ar[MAXN];
int par[MAXN];
int find(int x) {
  if (par[x] == x) return x;
  return par[x] = find(par[x]);
}
bool join(int a, int b) {
  a = find(a);
  b = find(b);
  if (a == b) return false;
  par[a] = b;
  return true;
}
void init() {
  for (int i = 0; i < N; ++i) {
    par[i] = i;
  }
}
int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> ar[i].first >> ar[i].second;
  }
  init();
  int res = N - 1;
  for (int i = 0; i < N; ++i) {
    for (int k = 0; k < i; ++k) {
      if (ar[i].first == ar[k].first || ar[i].second == ar[k].second) {
        res -= join(i, k);
      }
    }
  }
  cout << res << endl;
  return 0;
}
