#include <bits/stdc++.h>
using namespace std;
int n, x[105], y[105], m[105];
queue<int> qex, qey;
int main() {
  int i, j, c = 1, tx, ty;
  scanf("%d", &n);
  for (i = 0; i < n; ++i) scanf("%d %d", &x[i], &y[i]);
  for (i = 0; i < n; ++i) {
    if (m[i]) continue;
    m[i] = c;
    qex.push(x[i]);
    qey.push(y[i]);
    while (!qex.empty()) {
      tx = qex.front();
      ty = qey.front();
      qex.pop();
      qey.pop();
      for (j = 0; j < n; ++j) {
        if (m[j]) continue;
        if (x[j] == tx || y[j] == ty) {
          m[j] = c;
          qex.push(x[j]);
          qey.push(y[j]);
        }
      }
    }
    ++c;
  }
  printf("%d", c - 2);
  return 0;
}
