#include <bits/stdc++.h>
using namespace std;
vector<int> X(1003, 0);
vector<int> Y(1003, 0);
vector<int> visited(1003, 0);
int n;
void dfs(int check) {
  for (int i = 0; i < n; i++) {
    if ((X[i] == X[check] || Y[i] == Y[check]) && visited[i] == 0) {
      visited[i] = 1;
      dfs(i);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> X[i] >> Y[i];
  }
  int counter = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      visited[i] = 1;
      dfs(i);
      counter++;
    }
  }
  cout << counter - 1;
}
