#include <bits/stdc++.h>
using namespace std;
int ui[110];
vector<int> row[1001], col[1001];
struct point {
  int x, y;
} po[110];
int find(int p) {
  int q = p;
  while (ui[q]) q = ui[q];
  int tmp;
  while (ui[p]) {
    tmp = ui[p];
    ui[p] = q;
    p = tmp;
  }
  return q;
}
void uni(int p, int q) {
  int i = find(p), j = find(q);
  if (i != j) ui[i] = j;
}
void dfs(int p) {
  int len = row[po[p].x].size();
  for (int i = 0; i < len; i++) uni(p, row[po[p].x][i]);
  len = col[po[p].y].size();
  for (int i = 0; i < len; i++) uni(p, col[po[p].y][i]);
}
int main() {
  memset(ui, 0, sizeof(ui));
  int n;
  scanf("%d", &n);
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &po[i].x, &po[i].y);
    row[po[i].x].push_back(i);
    col[po[i].y].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    if (!ui[i]) {
      dfs(i);
    }
  }
  for (int i = 1; i <= n; i++)
    if (!ui[i]) ans++;
  printf("%d", ans);
  return 0;
}
