#include <bits/stdc++.h>
using namespace std;
int N, cnt;
pair<int, int> ver[103];
bool used[1003][1003];
void dfs(pair<int, int> a) {
  used[a.first][a.second] = true;
  for (int h = 0; h < N; h++)
    if (((a.first == ver[h].first) or (a.second == ver[h].second)) and
        (used[ver[h].first][ver[h].second] == false))
      dfs(ver[h]);
}
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) cin >> ver[i].first >> ver[i].second;
  for (int i = 0; i < N; i++) used[ver[i].first][ver[i].second] = false;
  for (int j = 0; j < N; j++)
    if (!used[ver[j].first][ver[j].second]) {
      dfs(ver[j]);
      cnt++;
    }
  cnt--;
  cout << cnt << endl;
  return 0;
}
