#include <bits/stdc++.h>
using namespace std;
long long int ans = -1;
void dfs(long long int x[], long long int y[], bool visited[],
         long long int index, long long int n) {
  visited[index] = true;
  for (long long int i = (0); i < (n); i++)
    if ((x[i] == x[index] || y[i] == y[index]) && visited[i] == 0) {
      dfs(x, y, visited, i, n);
    }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  {
    long long int n;
    cin >> n;
    long long int x[n], y[n];
    for (long long int i = (0); i < (n); i++) cin >> x[i] >> y[i];
    bool visited[n];
    memset(visited, 0, sizeof(visited));
    for (long long int i = (0); i < (n); i++)
      if (visited[i] == 0) {
        dfs(x, y, visited, i, n);
        ans++;
      }
    cout << ans << endl;
  }
}
