#include <bits/stdc++.h>
using namespace std;
void c() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long n;
vector<long long> x(1001), y(1001), vis(1001, 0);
void dfs(long long v) {
  vis[v] = 1;
  for (long long i = (0); i < (n); i++) {
    if ((x[i] == x[v] || y[i] == y[v]) && !vis[i]) dfs(i);
  }
}
int32_t main() {
  c();
  cin >> n;
  for (long long i = (0); i < (n); i++) cin >> x[i] >> y[i];
  long long cc = 0;
  for (long long i = (0); i < (n); i++) {
    if (!vis[i]) dfs(i), cc++;
  }
  cout << cc - 1 << "\n";
  return 0;
}
