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