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