#include <bits/stdc++.h>
using namespace std;
int n;
int x[111];
int y[111];
int current = 0;
int color[111];
void dfs(int j) {
  color[j] = current;
  for (int i = 1; i <= n; i++)
    if (color[i] == 0 && (x[i] == x[j] || y[i] == y[j])) dfs(i);
}
int main(void) {
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
  for (int i = 1; i <= n; i++)
    if (color[i] == 0) {
      current++;
      dfs(i);
    }
  cout << current - 1 << "\n";
  return 0;
}
