#include <bits/stdc++.h>
using namespace std;
const int N = 102;
bool visited[N];
vector<int> adj[N];
void DFS(int node) {
  if (visited[node]++) return;
  for (int i = 0; i < adj[node].size(); i++) DFS(adj[node][i]);
}
int main() {
  int n, ans = -1;
  cin >> n;
  int x[N], y[N];
  for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
  for (int i = 0; i < n; i++)
    for (int j = i + 1; j < n; j++)
      if (x[i] == x[j] || y[i] == y[j])
        adj[i].push_back(j), adj[j].push_back(i);
  for (int i = 0; i < n; i++)
    if (!visited[i]) ++ans, DFS(i);
  cout << ans;
}
