#include <bits/stdc++.h>
using namespace std;
long long int a;
int x[101], y[101];
bool vis[101];
void dfs(long long int n) {
  vis[n] = 1;
  for (long long int i = 1; i <= a; i++) {
    if (!vis[i] && (x[i] == x[n] || y[i] == y[n])) {
      dfs(i);
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int b, c;
  cin >> a;
  for (long long int i = 1; i <= a; i++) {
    cin >> b >> c;
    x[i] = b;
    y[i] = c;
  }
  long long int cnt = 0;
  for (long long int i = 1; i <= a; i++) {
    if (!vis[i]) {
      cnt++;
      dfs(i);
    }
  }
  cout << cnt - 1 << endl;
  return 0;
}
