#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int rk[N], p[N];
int n;
int getP(int a) { return (a == p[a] ? a : p[a] = getP(p[a])); }
void unite(int a, int b) {
  a = getP(a), b = getP(b);
  if (a == b) return;
  if (rk[a] < rk[b]) swap(a, b);
  p[b] = a;
  rk[a] += rk[b];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    p[i] = i;
    rk[i] = 1;
  }
  vector<pair<int, int> > vp;
  vp.push_back({0, 0});
  for (int i = 1; i <= n; ++i) {
    int x, y;
    cin >> x >> y;
    vp.push_back({x, y});
    for (int j = 1; j <= i - 1; ++j) {
      if (vp[i].first == vp[j].first or vp[i].second == vp[j].second)
        unite(i, j);
    }
  }
  set<int> s;
  for (int i = 1; i <= n; ++i) {
    s.insert(getP(i));
  }
  cout << int((s).size()) - 1;
}
