#include <bits/stdc++.h>
using namespace std;
class graph {
  unordered_map<int, list<int> > adjlist;

 public:
  void addedge(int u, int v) {
    adjlist[u].push_back(v);
    adjlist[v].push_back(u);
  }
  void comp_helper(unordered_map<int, bool>& vis, int node) {
    if (node == 0) return;
    vis[node] = true;
    for (auto i : adjlist[node]) {
      if (!vis[i]) comp_helper(vis, i);
    }
  }
  int comp() {
    unordered_map<int, bool> vis;
    int ans = 0;
    for (auto i : adjlist) {
      int node = i.first;
      if (!vis[node] && node != 0) {
        ans++;
        comp_helper(vis, node);
      }
    }
    return ans;
  }
};
int main() {
  graph g;
  int n;
  cin >> n;
  int x[n + 1], y[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
  }
  for (int i = 1; i <= n; i++) {
    g.addedge(i, 0);
    for (int j = i + 1; j <= n; j++) {
      if (x[i] == x[j] || y[i] == y[j]) g.addedge(i, j);
    }
  }
  int ans = g.comp() - 1;
  cout << ans;
}
