#include <bits/stdc++.h>
using namespace std;
int i, n, x[110], y[110], f[110], j, ans;
bool b[110];
int find(int x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = find(f[x]);
}
void Union(int x, int y) { f[find(x)] = find(y); }
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) f[i] = i;
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (find(i) != find(j))
        if (x[i] == x[j] || y[i] == y[j]) Union(i, j);
  memset(b, 0, sizeof(b));
  for (i = 1; i <= n; i++)
    if (!b[find(i)]) b[find(i)] = 1, ans++;
  printf("%d\n", ans - 1);
  return 0;
}
