#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int father[N];
int n;
void init() {
  for (int i = 1; i <= n; i++) {
    father[i] = i;
  }
}
int find(int x) {
  if (x != father[x]) {
    father[x] = find(father[x]);
  }
  return father[x];
}
void merge(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) {
    return;
  }
  father[x] = y;
}
struct Point {
  int x, y;
} p[N];
int main() {
  cin >> n;
  init();
  for (int i = 1; i <= n; i++) {
    cin >> p[i].x >> p[i].y;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (p[i].x == p[j].x || p[i].y == p[j].y) {
        merge(i, j);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (find(i) == i) {
      ans++;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
