#include <bits/stdc++.h>
using namespace std;
bool visited[1010][1010];
int x[1010], y[1010], n;
void dfs(int u, int v) {
  visited[u][v] = 1;
  for (int i = 0; i < n; i++) {
    if (!visited[x[i]][y[i]] && (x[i] == u || y[i] == v)) dfs(x[i], y[i]);
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) cin >> x[i] >> y[i];
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[x[i]][y[i]]) {
      dfs(x[i], y[i]);
      cnt++;
    }
  }
  cout << cnt - 1 << endl;
}
