#include <bits/stdc++.h>
using namespace std;
int n, visited[101];
int x[101], y[101];
void dfs(int a) {
  visited[a] = 1;
  for (int i = 0; i < n; i++)
    if ((x[i] == x[a] || y[i] == y[a]) && !visited[i]) dfs(i);
}
int main() {
  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]) dfs(i), ans++;
  cout << ans - 1 << endl;
  return 0;
}
