#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
const int INF = (int)1e9;
using namespace std;
template <class T>
T abs(T x) {
  return x < 0 ? -x : x;
}
template <class T>
T sqr(T x) {
  return x * x;
}
int n;
int x[1000], y[1000];
bool used[1000];
void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < n; ++i) {
    if ((!used[i]) && (x[v] == x[i] || y[v] == y[i])) {
      dfs(i);
    }
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> x[i] >> y[i];
  memset(used, false, sizeof(used));
  int ans = -1;
  for (int j = 0; j < n; ++j) {
    if (!used[j]) {
      ans++;
      dfs(j);
    }
  }
  cout << ans;
  return 0;
}
