#include <bits/stdc++.h>
using namespace std;
const int N = 102;
int vis[N], n;
vector<vector<int> > g;
pair<int, int> p[N];
void dfs(int u) {
  if (vis[u]) return;
  vis[u] = true;
  for (int i = 0; i < g[u].size(); ++i) dfs(g[u][i]);
}
int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 0, x, y; i < n; ++i) cin >> p[i].first >> p[i].second;
  g.resize(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j) continue;
      if (p[i].first == p[j].first || p[i].second == p[j].second)
        g[i].push_back(j);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      dfs(i);
      ++ans;
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
