#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const double eps1 = 1e-4;
int n;
int x[20000];
int y[20000];
bool vis[20000];
int k = 0;
void dfs(int p) {
  if (vis[p]) return;
  vis[p] = true;
  for (int i = 0; i < n; i++)
    if (x[i] == x[p] || y[i] == y[p]) dfs(i);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  for (int i = 0; i < n; i++) {
    if (!vis[i]) {
      ++k;
      dfs(i);
    }
  }
  cout << k - 1;
}
