#include <bits/stdc++.h>
using namespace std;
int x[110], y[110], f[110];
int n, ans;
void dfs(int i) {
  f[i] = 1;
  for (int j = 1; j <= n; j++)
    if (f[j] != 1 && (x[i] == x[j] || y[i] == y[j])) dfs(j);
  return;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
  for (int i = 1; i <= n; i++)
    if (f[i] != 1) {
      dfs(i);
      ans++;
    }
  cout << ans - 1 << endl;
  return 0;
}
