#include <bits/stdc++.h>
using namespace std;
int n;
bool used[100];
struct point {
  int x, y;
};
point m[100];
void dfs(int v) {
  used[v] = 1;
  for (int i = 0; i < n; i++)
    if ((used[i] == 0) && ((m[i].x == m[v].x) || (m[i].y == m[v].y))) dfs(i);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> m[i].x >> m[i].y;
  int cnt = -1;
  for (int i = 0; i < n; i++)
    if (used[i] == 0) {
      dfs(i);
      cnt++;
    }
  cout << cnt << endl;
  return 0;
}
