#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:106777216")
using namespace std;
vector<vector<int> > g(101);
int n, x[101], y[101], u[101];
void dfs(int v) {
  u[v] = 1;
  for (unsigned int i = 0; i < g[v].size(); i++) {
    if (!u[g[v][i]]) {
      dfs(g[v][i]);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x[i], &y[i]);
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (x[i] == x[j] || y[i] == y[j]) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  }
  int cc = 0;
  for (int i = 0; i < n; i++) u[i] = 0;
  for (int i = 0; i < n; i++) {
    if (!u[i]) {
      cc++;
      dfs(i);
    }
  }
  printf("%d", cc - 1);
}
