#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2;
int a[2][maxn];
vector<int> g[maxn];
bool visited[maxn];
void dfs(int cur) {
  visited[cur] = 1;
  for (int v : g[cur])
    if (!visited[v]) dfs(v);
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  int n, p = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[0][i] >> a[1][i];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      if (a[0][i] == a[0][j] || a[1][i] == a[1][j]) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
  for (int i = 0; i < n; i++)
    if (!visited[i]) {
      p++;
      dfs(i);
    }
  cout << p - 1 << endl;
}
