#include <bits/stdc++.h>
using namespace std;
bool sortbysec(const pair<long long, long long> &a,
               const pair<long long, long long> &b) {
  if (a.second == b.second) return a.first < b.first;
  return a.second < b.second;
}
long long parent[105];
void make_set(long long second) { parent[second] = second; }
long long getParent(long long second) {
  if (parent[second] == second) return second;
  return parent[second] = getParent(parent[second]);
}
void join(long long a, long long b) {
  a = getParent(a);
  b = getParent(b);
  if (a == b) {
    return;
  }
  parent[b] = a;
}
bool vis[105];
int32_t main() {
  long long n;
  cin >> n;
  vector<pair<long long, long long>> v;
  for (long long i = 0; i < n; i++) {
    long long x, y;
    cin >> x >> y;
    v.push_back(make_pair(x, y));
    make_set(i);
  }
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j < n; j++) {
      if (v[i].first == v[j].first || v[i].second == v[j].second) {
        join(i, j);
      }
    }
  }
  set<long long> ans;
  for (long long i = 0; i < n; i++) {
    ans.insert(getParent(i));
  }
  cout << ans.size() - 1;
  return 0;
}
