#include <bits/stdc++.h>
using namespace std;
void fio() {
  std::ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
}
void inputread() {}
int32_t main() {
  fio();
  long long int n, i, j;
  cin >> n;
  vector<pair<long long int, long long int>> c(n);
  vector<vector<long long int>> e(n);
  vector<long long int> visited(n, 0);
  for (i = 0; i < n; i++) {
    cin >> c[i].first >> c[i].second;
  }
  for (i = 0; i < n; i++) {
    for (j = i + 1; j < n; j++) {
      if ((c[i].first == c[j].first) || (c[i].second == c[j].second)) {
        e[i].push_back(j);
        e[j].push_back(i);
      }
    }
  }
  for (i = 0, j = 1; i < n; i++) {
    queue<long long int> qu;
    if (visited[i] == 0) {
      qu.push(i);
      visited[i] = j;
      while (!qu.empty()) {
        for (auto x : e[qu.front()]) {
          if (visited[x] == 0) qu.push(x);
        }
        visited[qu.front()] = j;
        qu.pop();
      }
      j++;
    }
  }
  cout << j - 2 << "\n";
  return 0;
}
