#include <bits/stdc++.h>
using namespace std;
long long timer = 1;
const long long ms = 200;
const long long inf = 1e8 + 5;
vector<long long> g[ms], dis(ms, inf), vis(ms);
vector<pair<long long, long long> > eds;
void add(long long u, long long v) {
  g[u].push_back(v);
  g[v].push_back(u);
}
void cleargraph() {
  for (long long i = 0; i < ms; i++) {
    g[i].clear();
  }
  dis.assign(ms, inf);
}
void dfs(long long u) {
  vis[u] = timer;
  for (auto v : g[u]) {
    if (vis[v] < timer) dfs(v);
  }
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<pair<long long, long long> > sd;
  for (long long i = 0; i < n; i++) {
    long long a, b;
    cin >> a >> b;
    sd.push_back({a, b});
    for (long long j = 0; j < i; j++) {
      if (sd[j].second == b or sd[j].first == a) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  }
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    if (vis[i] < timer) {
      ans++;
      dfs(i);
    }
  }
  cout << max(0ll, ans - 1) << '\n';
}
