#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
int dblcmp(double d) {
  if (fabs(d) < eps) return 0;
  return (d > 0) ? 1 : -1;
}
int n, m, T;
struct point {
  int x, y;
  void input() { scanf("%d%d", &x, &y); }
} p[200];
bool vis[200];
void dfs(int x) {
  vis[x] = 1;
  for (int i = 0; i < n; ++i)
    if (!vis[i] && (p[i].x == p[x].x || p[i].y == p[x].y)) dfs(i);
}
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    p[i].input();
  }
  int cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      ++cnt;
      dfs(i);
    }
  }
  printf("%d\n", cnt - 1);
}
