#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[105];
int n, v[1005][1005] = {0};
int dfs(pair<int, int> temp) {
  for (int i = 0; i < n; i++) {
    if (temp.first == a[i].first || temp.second == a[i].second) {
      if (v[a[i].first][a[i].second] != 1) {
        v[a[i].first][a[i].second] = 1;
        dfs(a[i]);
      }
    }
  }
  return 0;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first >> a[i].second;
  }
  int c = -1;
  for (int i = 0; i < n; i++) {
    if (v[a[i].first][a[i].second] != 1) {
      c++;
      v[a[i].first][a[i].second] = 1;
      dfs(a[i]);
    }
  }
  cout << c;
}
