#include <bits/stdc++.h>
using namespace std;
int n;
int x[105], y[105], f[105];
int getf(int x) { return (f[x] == x) ? x : (f[x] = getf(f[x])); }
void merge(int x, int y) {
  int t1 = getf(x), t2 = getf(y);
  if (t1 != t2) f[t1] = t2;
}
int solve() {
  int res = 0;
  for (int i = 1; i <= n; i++)
    if (f[i] == i) res++;
  return res;
}
void init() {
  for (int i = 1; i <= n; i++) f[i] = i;
}
int main() {
  scanf("%d", &n);
  init();
  for (int i = 1; i <= n; i++) scanf("%d %d", x + i, y + i);
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (x[i] == x[j] || y[i] == y[j]) merge(i, j);
  printf("%d\n", solve() - 1);
  return 0;
}
