#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, ans;
pair<int, int> e[N];
bool vis[N], g[N][N];
void dfs(int x) {
  vis[x] = 1;
  for (int j = 1; j <= n; j++) {
    if (!vis[j] && g[x][j]) {
      dfs(j);
    }
  }
}
inline void Solve() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      if (e[i].first == e[j].first || e[i].second == e[j].second) {
        g[i][j] = g[j][i] = 1;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i), ans++;
  printf("%d", ans - 1);
}
inline void Init() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &e[i].first, &e[i].second);
  }
}
int main() {
  Init();
  Solve();
  return 0;
}
