#include <bits/stdc++.h>
using namespace std;
int n, points[100][2];
bool visited[1001][1001];
void dfs(int x, int y) {
  if (visited[x][y]) return;
  visited[x][y] = true;
  for (int i = 0; i < n; i++)
    if (points[i][0] == x || points[i][1] == y) dfs(points[i][0], points[i][1]);
}
int main() {
  int counter = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> points[i][0] >> points[i][1];
  for (int i = 0; i < n; i++)
    if (!visited[points[i][0]][points[i][1]])
      dfs(points[i][0], points[i][1]), counter++;
  cout << counter - 1 << endl;
  return 0;
}
