#include <bits/stdc++.h>
using namespace std;
int main() {
  int pas = 0, tem, tem1;
  int n;
  cin >> n;
  vector<int> parent(n + 1, 0);
  int x, y;
  vector<vector<int> > s(n + 1, (vector<int>(3)));
  for (int i = 1; i <= n; i++) {
    cin >> s[i][1] >> s[i][2];
  }
  for (int i = 1; i <= n; i++) {
    if (parent[i] == 0) {
      parent[i] = i;
      pas++;
    }
    for (int j = i + 1; j <= n; j++) {
      if ((s[i][1] == s[j][1]) || (s[i][2] == s[j][2]))
        if (parent[j] == 0)
          parent[j] = parent[i];
        else {
          if (parent[i] != parent[j]) {
            tem = parent[j];
            tem1 = parent[i];
            for (int h = 1; h <= n; h++)
              if (parent[h] == tem) parent[h] = tem1;
            pas--;
          }
        }
    }
  }
  pas--;
  cout << pas;
  return 0;
}
