#include <bits/stdc++.h>
using namespace std;
bool vis[1005][1005];
int x[105];
int y[105];
int n;
void dfs(int x1, int y1) {
  vis[x1][y1] = true;
  for (int i = 0; i < n; i++) {
    if (!(x[i] == x1 && y[i] == y1) && (x[i] == x1 || y[i] == y1) &&
        !vis[x[i]][y[i]])
      dfs(x[i], y[i]);
  }
}
int main() {
  cin >> n;
  int i, j;
  int res = 0;
  memset(vis, false, sizeof(vis));
  for (i = 0; i < n; i++) cin >> x[i] >> y[i];
  for (i = 0; i < n; i++) {
    if (!vis[x[i]][y[i]]) {
      dfs(x[i], y[i]);
      res++;
    }
  }
  cout << res - 1 << "\n";
  return 0;
}
