#include <bits/stdc++.h>
using namespace std;
const int N = 4000;
bool visited[N];
int n, x[N], y[N];
bool isConnected(int i, int j) {
  if (i == j) return false;
  return x[i] == x[j] || y[i] == y[j];
}
void dfs(int i) {
  visited[i] = true;
  for (int j = 0; j < n; ++j)
    if (isConnected(i, j) && !visited[j]) dfs(j);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> x[i] >> y[i];
  int ans = 0;
  for (int i = 0; i < n; ++i)
    if (!visited[i]) ++ans, dfs(i);
  ans--;
  cout << ans << endl;
  return 0;
}
