#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;
const int nmax = 1e5 + 10;
const int eps = 1e-8;
map<pair<int, int>, pair<int, int> > parent;
map<pair<int, int>, int> sz;
int n;
pair<int, int> find_rep(pair<int, int> k) {
  while (k != parent[k]) {
    parent[k] = parent[parent[k]];
    k = parent[k];
  }
  return k;
}
void Merge(pair<int, int> u, pair<int, int> v) {
  pair<int, int> rep1 = find_rep(u);
  pair<int, int> rep2 = find_rep(v);
  if (rep1 == rep2) return;
  if (sz[rep1] > sz[rep2]) swap(rep1, rep2);
  sz[rep2] += sz[rep1];
  sz[rep1] = 0;
  parent[rep1] = parent[rep2];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;
  cin >> n;
  vector<pair<int, int> > v(n);
  for (int i = 0; i < n; i++) cin >> v[i].first >> v[i].second;
  for (int i = 0; i < n; i++) {
    parent[v[i]] = v[i];
    sz[v[i]] = 1;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (v[i].first == v[j].first || v[i].second == v[j].second)
        Merge(v[i], v[j]);
    }
  }
  int ans = 0;
  for (auto i : parent) {
    if (i.first == i.second) ans++;
  }
  if (ans)
    cout << ans - 1 << "\n";
  else
    cout << ans << "\n";
  return 0;
}
