#include <bits/stdc++.h>
using namespace std;
void cass() {
  ios::sync_with_stdio(0);
  cin.tie(0);
}
const int N = 104, L = 1e3 + 2;
int n, dem;
pair<int, int> a[N];
bool ds[N][N], check[N];
void read() {
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i].first >> a[i].second;
  for (int i = 1; i <= n - 1; ++i)
    for (int j = i + 1; j <= n; ++j)
      if (a[i].first == a[j].first || a[i].second == a[j].second)
        ds[i][j] = ds[j][i] = 1;
}
void DFS(int u) {
  check[u] = 1;
  for (int i = 1; i <= n; ++i)
    if (ds[u][i] && !check[i]) DFS(i);
}
void solve() {
  for (int i = 1; i <= n; ++i)
    if (!check[i]) {
      DFS(i);
      dem++;
    }
  cout << dem - 1;
}
int main() {
  cass();
  read();
  solve();
  return 0;
}
