#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const double PI = 3.141592653589793238463;
const int MAXN = 1e7 + 5;
long long fastPow(long long x, long long b) {
  if (b == 0)
    return 1;
  else if (!(b & 1)) {
    return fastPow(x * x, b / 2);
  } else
    return x * fastPow(x * x, (b - 1) / 2);
}
int n, x[2000], y[2000], vis[2000], ans = 0;
void dfs(int a) {
  vis[a] = 1;
  for (int i = 1; i <= n; i++) {
    if ((x[a] == x[i] || y[a] == y[i]) && !vis[i]) dfs(i);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i);
      ans++;
    }
  }
  cout << --ans << '\n';
  return 0;
}
