#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
  return 0;
}
int l[100011];
int f(int x) { return l[x] == x ? x : l[x] = f(l[x]); }
void solve() {
  long long n, arr[2][10000];
  memset(arr, 0, sizeof(arr));
  cin >> n;
  for (int i = 0; i < 10000; i++) {
    l[i] = i;
  }
  map<long long, long long> mp;
  for (int i = 0; i < n; i++) {
    cin >> arr[0][i] >> arr[1][i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (arr[0][i] == arr[0][j]) {
        l[f(j)] = f(i);
      }
      if (arr[1][i] == arr[1][j]) {
        l[f(j)] = f(i);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    mp[f(i)]++;
  }
  cout << mp.size() - 1;
}
