#include <bits/stdc++.h>
using namespace std;
vector<bool> used(110, false);
vector<long long int> x(110), y(110);
long long int n;
void dfs(long long int s) {
  used[s] = true;
  for (long long int i = 0; i < n; i++) {
    if (used[i] == true) continue;
    if (x[i] == x[s] || y[i] == y[s]) {
      dfs(i);
    }
  }
}
void solve() {
  cin >> n;
  long long int ans = -1;
  for (long long int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  for (long long int i = 0; i < n; i++) {
    if (used[i] == false) {
      dfs(i);
      ans++;
    }
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
