#include <bits/stdc++.h>
using namespace std;
int n, x[105], y[105], p[105];
int find(int u) { return p[u] == u ? u : p[u] = find(p[u]); }
set<int> s;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) p[i] = i;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    for (int j = 0; j < i; j++)
      if (x[i] == x[j] || y[i] == y[j]) {
        int x = find(i), y = find(j);
        if (x != y) p[x] = y;
      }
  }
  for (int i = 0; i < n; i++) s.insert(find(i));
  int k = s.size();
  k--;
  cout << k << endl;
  return 0;
}
