#include <bits/stdc++.h>
using namespace std;
int grid[1005][1005];
int node, n;
vector<pair<int, int> > v;
int boss[1000006];
int st;
void pre(int n) {
  for (int i = 0; i <= n; i++) {
    boss[i] = i;
  }
  st = n;
}
int root(int x) {
  if (boss[x] == x) return x;
  return boss[x] = root(boss[x]);
}
void Union(int u, int v) {
  int x, y;
  x = root(u);
  y = root(v);
  if (x != y) {
    boss[x] = y;
    st--;
  }
}
int isRel(int xi, int yi, int xj, int yj) {
  if (xi == xj || yi == yj) return 1;
  return 0;
}
int main() {
  int i, j, x, y;
  cin >> n;
  pre(n);
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
    grid[x][y] = ++node;
  }
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if (isRel(v[i].first, v[i].second, v[j].first, v[j].second)) {
        Union(grid[v[i].first][v[i].second], grid[v[j].first][v[j].second]);
      }
    }
  }
  cout << st - 1 << "\n";
  return 0;
}
