#include <bits/stdc++.h>
using namespace std;
pair<int, int> point[207];
bool used[207];
int ans;
int N;
void DFS(int n) {
  used[n] = 1;
  for (int i = 0; i < N; i++)
    if (!used[i] && (point[i].first == point[n].first ||
                     point[i].second == point[n].second))
      DFS(i);
}
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) cin >> point[i].first >> point[i].second;
  for (int i = 0; i < N; i++)
    if (!used[i]) {
      ans++;
      DFS(i);
    }
  cout << ans - 1;
  return 0;
}
