#include <bits/stdc++.h>
using namespace std;
int g[1000], used[1000], x, y, n, t;
void dfs(int v) {
  used[v] = 1;
  for (int k = 1; k <= n; k++)
    if (g[v] / 1000 == g[k] / 1000 || g[v] % 1000 == g[k] % 1000)
      if (used[k] == 0) {
        dfs(k);
      }
}
int main() {
  cin >> n;
  for (int k = 1; k <= n; k++) {
    cin >> x >> y;
    g[k] = x + y * 1000;
  }
  for (int k = 1; k <= n; k++) {
    if (used[k] == 0) {
      dfs(k);
      t++;
    }
  }
  cout << t - 1;
  return 0;
}
