#include <bits/stdc++.h>
using namespace std;
int p[1000000], n, ans = -1;
pair<int, int> v[100];
int root(int x) {
  if (x == p[x]) return x;
  return p[x] = root(p[x]);
}
void unionSets(int a, int b) {
  a = root(a);
  b = root(b);
  if (a == b) return;
  if (rand() & 1) swap(a, b);
  p[a] = b;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> v[i].first >> v[i].second;
    p[i] = i;
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (v[i].first == v[j].first || v[i].second == v[j].second)
        if (root(i) != root(j)) unionSets(i, j);
  for (int i = 0; i < n; ++i) ans += i == root(i);
  cout << ans << endl;
  return 0;
}
