#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int n, X[maxn], Y[maxn], fa[maxn];
int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); }
bool check(int i, int j) { return (X[i] == X[j] || Y[i] == Y[j]); }
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    fa[i] = i;
    scanf("%d%d", &X[i], &Y[i]);
  }
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      if (check(i, j)) {
        int a = findset(i), b = findset(j);
        if (a != b) fa[a] = b;
      }
  int ans = 0;
  for (int i = 0; i < n; i++)
    if (fa[i] == i) ans++;
  printf("%d\n", ans - 1);
}
