#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
};
int main() {
  int n;
  while (scanf("%d", &n) != EOF) {
    int i, j, cnt = 0;
    node a[110];
    for (i = 0; i < n; i++) {
      scanf("%d%d", &a[i].x, &a[i].y);
    }
    bool u[110] = {0};
    queue<int> q;
    for (i = 0; i < n; i++) {
      if (u[i]) continue;
      u[i] = 1;
      q.push(i);
      while (!q.empty()) {
        node t = a[q.front()];
        q.pop();
        for (j = 0; j < n; j++) {
          if (u[j] == 0 && (t.x == a[j].x || t.y == a[j].y)) {
            u[j] = 1;
            q.push(j);
          }
        }
      }
      cnt += 1;
    }
    printf("%d\n", cnt - 1);
  }
  return 0;
}
