#include <bits/stdc++.h>
using namespace std;
struct ln {
  int x, y;
} s[110];
bool flag[110];
int n;
void dfs(int x) {
  flag[x] = 1;
  for (int i = 1; i < n; i++)
    if (!flag[i] && (s[i].x == s[x].x || s[i].y == s[x].y)) dfs(i);
}
int main() {
  while (cin >> n) {
    for (int i = 0; i < n; i++) cin >> s[i].x >> s[i].y;
    memset(flag, 0, sizeof(flag));
    int cnt = 0;
    for (int i = 0; i < n; i++)
      if (!flag[i]) {
        dfs(i);
        cnt++;
      }
    cout << cnt - 1 << endl;
  }
  return 0;
}
