#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[200];
bool vi[200];
void dfs(int x) {
  if (vi[x]) return;
  vi[x] = 1;
  for (int y = (1); y <= (n); y++)
    if (a[x].first == a[y].first || a[x].second == a[y].second) dfs(y);
}
int main() {
  int ans = 0;
  cin >> n;
  for (int i = (1); i <= (n); i++) cin >> a[i].first >> a[i].second;
  for (int i = (1); i <= (n); i++)
    if (!vi[i]) {
      ans++;
      dfs(i);
    }
  cout << ans - 1 << endl;
  return 0;
}
