#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[200];
int p[200] = {};
void dfs(int x) {
  p[x] = 1;
  for (int i = 0; i < n; i++) {
    if (p[i] == 1) continue;
    if (a[i].first == a[x].first || a[i].second == a[x].second) dfs(i);
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[i] = make_pair(x, y);
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] == 0) {
      cnt += 1;
      dfs(i);
    }
  }
  printf("%d", cnt - 1);
  return 0;
}
