#include <bits/stdc++.h>
using namespace std;
bool visit[1001][1001];
bool pts[1001][1001];
void dfs(int x, int y, int n) {
  for (int i = 1; i <= 1000; i++) {
    if (pts[i][y] && !visit[i][y]) {
      visit[i][y] = true;
      dfs(i, y, n);
    }
  }
  for (int i = 1; i <= 1000; i++) {
    if (pts[x][i] && !visit[x][i]) {
      visit[x][i] = true;
      dfs(x, i, n);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, a, b, ans = 0;
  cin >> n;
  memset(pts, 0, sizeof(pts));
  memset(visit, 0, sizeof(visit));
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    pts[a][b] = true;
  }
  for (int i = 1; i <= 1000; i++) {
    for (int j = 1; j <= 1000; j++) {
      if (!visit[i][j] && pts[i][j]) {
        visit[i][j] = true;
        ans++;
        dfs(i, j, n);
      }
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
