#include <bits/stdc++.h>
using namespace std;
;
int x[1009];
int y[1009];
int p[1009];
int getp(int i) {
  if (p[i]) return p[i] = getp(p[i]);
  return i;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, c;
  c = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> x[i] >> y[i];
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (x[i] == x[j] || y[i] == y[j])
        if (getp(i) != getp(j)) p[getp(i)] = getp(j);
  for (int i = 1; i <= n; i++)
    if (p[i] == 0) c++;
  cout << c - 1 << '\n';
  cout.flush();
}
