#include <bits/stdc++.h>
using namespace std;
vector<int> f(2000);
int num_sets;
void init_set(const int& n) {
  for (int i = 0; i <= n; ++i) f[i] = i;
  num_sets = n;
}
int find_set(int i) { return (f[i] == i) ? i : f[i] = find_set(f[i]); }
bool is_same_set(int i, int j) { return find_set(i) == find_set(j); }
void union_set(int i, int j) {
  if (!is_same_set(i, j)) {
    f[find_set(i)] = find_set(j);
    --num_sets;
  }
}
bool share_coordinates(const pair<int, int>& i, const pair<int, int>& j) {
  return i.first == j.first || i.second == j.second;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  vector<pair<int, int> > v;
  while (cin >> n) {
    init_set(n);
    v.resize(n);
    for (int i = 0; i < n; ++i) cin >> v[i].first >> v[i].second;
    for (int i = 0; i < (int)v.size(); ++i)
      for (int j = 0; j < (int)v.size(); ++j)
        if (share_coordinates(v[i], v[j])) union_set(i, j);
    cout << num_sets - 1 << '\n';
  }
  return 0;
}
