#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
vector<ll> parent(1001, -1);
ll find(ll a) {
  if (parent[a] == a)
    return a;
  else
    return parent[a] = find(parent[a]);
}
void merge(ll a, ll b) {
  a = find(a);
  b = find(b);
  parent[a] = b;
}
void solveCP311() {
  ll n;
  cin >> n;
  vector<ll> x(n + 1), y(+1);
  for (ll i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    parent[i] = i;
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      if (x[i] == x[j] || y[i] == y[j]) {
        merge(i, j);
      }
    }
  }
  ll ans = -1;
  for (ll i = 1; i <= n; i++) {
    if (find(i) == i) ans++;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solveCP311();
  }
  return 0;
}
