#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int> > v;
vector<int> adj[102];
bool vis[102];
void dfs(int u) {
  vis[u] = true;
  vector<int>::iterator it;
  for (it = adj[u].begin(); it != adj[u].end(); it++) {
    if (!vis[*it]) dfs(*it);
  }
}
int main() {
  int k = 0, n, i, j, x, y;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }
  vector<pair<int, int> >::iterator it, it1;
  i = 1;
  for (it = v.begin(); it != v.end(); i++, it++) {
    j = 1;
    for (it1 = v.begin(); it1 != v.end(); j++, it1++) {
      if (it1 != it) {
        if ((*it).first == (*it1).first || (*it).second == (*it1).second)
          adj[i].push_back(j);
      }
    }
  }
  vector<int>::iterator it3;
  for (i = 1; i <= n; i++) vis[i] = false;
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      k++;
      dfs(i);
    }
  }
  cout << k - 1 << endl;
}
