#include <bits/stdc++.h>
using namespace std;
bool mark[100];
vector<pair<int, int> > coors;
void DFS(int ver) {
  mark[ver] = true;
  for (int i = 0; i < coors.size(); i++)
    if ((coors[ver].first == coors[i].first ||
         coors[ver].second == coors[i].second) &&
        !mark[i])
      DFS(i);
}
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  coors.resize(n);
  for (int i = 0; i < n; i++) {
    pair<int, int> tmp;
    cin >> tmp.first >> tmp.second;
    coors[i] = tmp;
  }
  int ans = -1;
  for (int i = 0; i < n; i++) {
    if (!mark[i]) {
      ans++;
      DFS(i);
    }
  }
  cout << ans << endl;
}
