#include <bits/stdc++.h>
using namespace std;
int n, a[105][2], counter = -1;
vector<int> adj[105];
bool visited[105];
void dfs(int cur) {
  visited[cur] = 1;
  for (int i = 0; i < adj[cur].size(); i++) {
    if (visited[adj[cur][i]] == 0) dfs(adj[cur][i]);
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j == i) continue;
      if (a[i][0] == a[j][0] || a[i][1] == a[j][1]) {
        adj[j].push_back(i);
        adj[i].push_back(j);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      counter++;
      dfs(i);
    }
  }
  cout << counter << endl;
  return 0;
}
