#include <bits/stdc++.h>
using namespace std;
int n, visited[105];
vector<pair<int, int> > v;
void DFS(int pos) {
  if (visited[pos]) return;
  visited[pos] = 1;
  for (int i = 0; i < n; i++) {
    if (v[i].first == v[pos].first || v[i].second == v[pos].second) DFS(i);
  }
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back({x, y});
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!visited[i]) {
      cnt++;
      DFS(i);
    }
  }
  cout << cnt - 1 << "\n";
}
int main() {
  int t = 1;
  while (t--) solve();
  return 0;
}
