#include <bits/stdc++.h>
using namespace std;
void dfs(vector<pair<long long, long long>> &p, vector<bool> &vis, long long x,
         long long N) {
  vis[x] = true;
  for (long long i = 0; i < N; i++)
    if (!vis[i] && (p[i].first == p[x].first || p[i].second == p[x].second))
      dfs(p, vis, i, N);
}
int32_t main() {
  long long N;
  cin >> N;
  vector<bool> vis(N, false);
  vector<pair<long long, long long>> p(N);
  for (long long n = 0; n < N; n++) cin >> p[n].first >> p[n].second;
  long long cnt = 0;
  for (long long i = 0; i < N; i++) {
    if (!vis[i]) {
      dfs(p, vis, i, N);
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
  return 0;
}
