#include <bits/stdc++.h>
using namespace std;
const int N = 111;
int parent[N], n;
pair<int, int> xy[N];
void make_set(int v) { parent[v] = v; }
int find_set(int v) {
  if (v == parent[v]) return v;
  return find_set(parent[v]);
}
void union_sets(int a, int b) {
  a = find_set(a);
  b = find_set(b);
  if (a != b) parent[b] = a;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> xy[i].first >> xy[i].second;
    make_set(i);
    for (int j = 0; j < i; j++) {
      if (xy[j].first == xy[i].first || xy[j].second == xy[i].second) {
        union_sets(j, i);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans += parent[i] == i;
  }
  cout << ans - 1;
  return 0;
}
