#include <bits/stdc++.h>
using namespace std;
struct p {
  int i, j;
} point[105];
int f[105];
int findset(int v) {
  if (f[v] != v) f[v] = findset(f[v]);
  return f[v];
}
void Union(int i, int j) {
  i = findset(i);
  j = findset(j);
  if (i != j) f[i] = j;
}
int main() {
  int n, i, j, r;
  while (cin >> n) {
    for (i = 1; i <= n; i++) {
      cin >> point[i].i >> point[i].j;
      f[i] = i;
    }
    for (i = 1; i <= n; i++) {
      for (j = i + 1; j <= n; j++) {
        if (point[i].i == point[j].i || point[i].j == point[j].j) Union(i, j);
      }
    }
    r = 0;
    for (i = 1; i <= n; i++)
      if (f[i] == i) r = r + 1;
    cout << r - 1 << endl;
  }
  return 0;
}
