#include <bits/stdc++.h>
using namespace std;
int n, t;
const int MAXN = 1e2 + 10;
bool mark[1000][1000];
pair<int, int> p[MAXN];
void dfs(int u, int v) {
  mark[u][v] = true;
  for (int i = 0; i < n; i++)
    if ((p[i].first == u || p[i].second == v) && !mark[p[i].first][p[i].second])
      dfs(p[i].first, p[i].second);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) cin >> p[i].first >> p[i].second;
  for (int i = 0; i < n; i++)
    if (!mark[p[i].first][p[i].second]) t++, dfs(p[i].first, p[i].second);
  cout << t - 1;
  return 0;
}
