#include <bits/stdc++.h>
int x[105], y[105];
bool vis[105];
void dfs(int xx, int n) {
  int i;
  for (i = 1; i <= n; i++)
    if (!vis[i] && (x[xx] == x[i] || y[xx] == y[i])) {
      vis[i] = true;
      dfs(i, n);
    }
}
int main() {
  int n, ans, i;
  while (scanf("%d", &n) != EOF) {
    for (i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]);
    memset(vis, false, sizeof(vis));
    ans = 0;
    for (i = 1; i <= n; i++)
      if (!vis[i]) {
        ans++;
        vis[i] = true;
        dfs(i, n);
      }
    printf("%d\n", ans - 1);
  }
  return 0;
}
