#include <bits/stdc++.h>
using namespace std;
map<pair<long long int, long long int>, pair<long long int, long long int> >
    parent;
map<pair<long long int, long long int>, long long int> size;
long long int INF = 1e18;
void initialise() {
  long long int i, j, k;
  for (i = 0; i <= 1000; i++) {
    for (j = 0; j <= 1000; j++) {
      parent[{i, j}] = {i, j};
      size[{i, j}] = 1;
    }
  }
}
pair<long long int, long long int> root(pair<long long int, long long int> x) {
  while (x != parent[x]) {
    parent[x] = parent[parent[x]];
    x = parent[x];
  }
  return x;
}
void union1(pair<long long int, long long int> x,
            pair<long long int, long long int> y) {
  pair<long long int, long long int> i, j;
  i = root(x);
  j = root(y);
  if (i != j) {
    if (size[i] > size[j]) {
      size[i] = size[i] + size[j];
      parent[j] = i;
    } else {
      size[j] = size[j] + size[i];
      parent[i] = j;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int i, j, k, l, m, n, o, p, ans, cnt, x, y, mini;
  cin >> n;
  pair<long long int, long long int> A[n];
  for (i = 0; i < n; i++) {
    cin >> j >> k;
    A[i] = {j, k};
  }
  initialise();
  for (i = 1; i < n; i++) {
    for (j = 0; j < i; j++) {
      if (A[j].first - A[i].first == 0 || A[j].second - A[i].second == 0) {
        if (root(A[j]) != root(A[i])) {
          union1(A[i], A[j]);
        }
      }
    }
  }
  long long int cost = 0;
  for (i = 0; i < n; i++) {
    if (root(A[i]) == A[i]) {
      cost++;
    }
  }
  cout << cost - 1 << endl;
}
