#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 2e5 + 100;
const int INF = 0x3f3f3f3f;
inline long long fpow(long long a, long long b) {
  long long r = 1, t = a;
  while (b) {
    if (b & 1) r = (r * t) % MOD;
    b >>= 1;
    t = (t * t) % MOD;
  }
  return r;
}
int f[110], n;
pair<int, int> s[110];
int find_(int x) {
  if (x != f[x]) x = f[x] = find_(f[x]);
  return x;
}
void unite(int x, int y) {
  x = find_(x);
  y = find_(y);
  if (x != y) f[x] = y;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= n; i++) scanf("%d %d", &s[i].first, &s[i].second);
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      int x1 = s[i].first, y1 = s[i].second;
      int x2 = s[j].first, y2 = s[j].second;
      if (x1 == x2 || y1 == y2) unite(i, j);
    }
  }
  set<int> st;
  for (int i = 1; i <= n; i++) st.insert(find_(i));
  cout << ((int)(st).size()) - 1 << endl;
  return 0;
}
