#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const double eps = 1e-11;
const int INF = 1 << 30;
const int dx[] = {
    1,
    0,
    -1,
    0,
};
const int dy[] = {
    0,
    1,
    0,
    -1,
};
int g[100][100], n;
int bfs() {
  int cnt = 0, x, v[100];
  memset(v, 0, sizeof(v));
  queue<int> q;
  for (int j = 0; j < n; j++) {
    if (v[j] == 0) {
      cnt++;
      q.push(j);
      v[j] = 1;
      while (!q.empty()) {
        x = q.front();
        q.pop();
        for (int i = 0; i < n; i++) {
          if (g[x][i] && v[i] == 0) {
            v[i] = 1;
            q.push(i);
          }
        }
      }
    }
  }
  return cnt;
}
int main() {
  int i, j, x, y, res, cnt = 0;
  pair<int, int> c[100];
  memset(c, 0, sizeof(c));
  memset(g, 0, sizeof(g));
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    c[i].first = x;
    c[i].second = y;
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (i != j && (c[i].first == c[j].first || c[i].second == c[j].second)) {
        g[i][j] = g[j][i] = 1;
      }
    }
  }
  cnt = bfs();
  res = cnt - 1;
  cout << res << endl;
  return 0;
}
