#include <bits/stdc++.h>
using namespace std;
void InitFiles() {
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
}
bool mark[100];
int n, x[100], y[100];
void In() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i];
  }
}
void dfs(int v) {
  mark[v] = true;
  for (int i = 0; i < n; ++i) {
    if (mark[i]) {
      continue;
    }
    if (x[i] == x[v] || y[i] == y[v]) {
      dfs(i);
    }
  }
}
void Solve() {
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (!mark[i]) {
      dfs(i);
      ++cnt;
    }
  }
  cout << cnt - 1 << endl;
}
int main() {
  In();
  Solve();
  return 0;
}
