#include <bits/stdc++.h>
using namespace std;
int n;
int points[100][2];
vector<int> adjacents[100];
int groups[100];
void FormGroups(int node, int group) {
  if (groups[node] == 0) groups[node] = group;
  for (int i = 0; i < adjacents[node].size(); i++) {
    int child = adjacents[node][i];
    if (groups[child] == 0) {
      groups[child] = group;
      FormGroups(child, group);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> points[i][0] >> points[i][1];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (points[i][0] == points[j][0] || points[i][1] == points[j][1]) {
        adjacents[i].push_back(j);
        adjacents[j].push_back(i);
      }
    }
  }
  int currentgroup = 0;
  for (int i = 0; i < n; i++)
    if (groups[i] == 0) {
      currentgroup++;
      FormGroups(i, currentgroup);
    }
  cout << currentgroup - 1;
}
