#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int inf = 1000000000;
const int N = 100007, M = N;
long long i, j, cnt, par;
vector<int> vis(5001);
vector<pair<int, int> > vec;
void dfs(int v) {
  vis[v] = 1;
  for (int i = 0; i < vec.size(); i++) {
    if (!vis[i] &&
        (vec[i].first == vec[v].first || vec[i].second == vec[v].second)) {
      dfs(i);
    }
  }
}
void solve() {
  int n, x, y;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    vec.push_back({x, y});
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i]) dfs(i), ans++;
  }
  cout << ans - 1 << "\n";
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
