#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 555;
const int M = 2e31 - 1;
int n;
int a[111], b[111];
vector<int> v[111];
int c[111];
int ans;
void man(int i) {
  c[i]++;
  for (int j = 0; j < (int)v[i].size(); ++j) {
    if (c[v[i][j]] == 0) {
      man(v[i][j]);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i] >> b[i];
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      if (a[i] == a[j] || b[i] == b[j]) {
        v[i].push_back(j);
        v[j].push_back(i);
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (c[i] == 0) {
      man(i);
      ans++;
    }
  }
  cout << ans - 1 << '\n';
  return 0;
}
