#include <bits/stdc++.h>
using namespace std;
const int N = 1234567;
struct point {
  int x;
  int y;
};
point a[N];
int parent[N];
int key(point p) { return p.x * 1000 + p.y; }
int find_parent(int v) {
  if (v != parent[v]) {
    parent[v] = find_parent(parent[v]);
  }
  return parent[v];
}
void disjoint_set_union(int u, int v) {
  u = find_parent(u);
  v = find_parent(v);
  parent[u] = v;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < N; i++) {
    parent[i] = i;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &a[i].x, &a[i].y);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i].x == a[j].x || a[i].y == a[j].y) {
        disjoint_set_union(key(a[i]), key(a[j]));
      }
    }
  }
  set<int> s;
  for (int i = 0; i < n; i++) {
    s.insert(find_parent(key(a[i])));
  }
  cout << (int)s.size() - 1 << endl;
  return 0;
}
