#include <bits/stdc++.h>
using namespace std;
pair<int, int> pr[110];
list<int> adj[110];
int visited[110], counter = 0;
void dfsv(int v, int n) {
  visited[v] = 1;
  list<int>::iterator it;
  for (it = adj[v].begin(); it != adj[v].end(); it++) {
    if (visited[*it] == 0) {
      dfsv(*it, n);
    }
  }
}
int dfs(int n) {
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      visited[i] = 1;
      dfsv(i, n);
      counter++;
    }
  }
  return counter;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> pr[i].first >> pr[i].second;
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (pr[i].first == pr[j].first || pr[i].second == pr[j].second) {
        adj[i].push_back(j);
        adj[j].push_back(i);
      }
    }
  }
  int ans = dfs(n);
  cout << ans - 1 << "\n";
}
