#include <bits/stdc++.h>
using namespace std;
const int mod = (int)1e9 + 7;
const int MAXN = 1e5 + 12;
int n;
int x[105], y[105];
map<pair<int, int>, int> mpp;
pair<int, int> p[105];
vector<int> g[1005];
int used[1005];
void dfs(int v) {
  used[v] = 1;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (!used[to]) dfs(to);
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL), cout.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    p[i] = make_pair(x[i], y[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      if (x[i] == x[j] || y[i] == y[j]) g[i].push_back(j);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    if (!used[i]) dfs(i), ans++;
  }
  cout << ans - 1;
  return 0;
}
