#include <bits/stdc++.h>
using namespace std;
long long int n;
vector<pair<long long int, long long int>> a;
vector<long long int> joined;
void join(long long int x) {
  joined[x] = 1;
  for (int i = 0; i < n; i++)
    if (!joined[i] and (a[i].first == a[x].first or a[i].second == a[x].second))
      join(i);
}
void swagWaalaFunction() {
  cin >> n;
  a.resize(n);
  joined.resize(n, 0);
  for (auto &[u, v] : a) cin >> u >> v;
  long long int ans = 0;
  for (int i = 0; i < n; i++)
    if (!joined[i]) join(i), ans++;
  cout << ans - 1 << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t = 1;
  while (t--) swagWaalaFunction();
  return 0;
}
