#include <bits/stdc++.h>
using namespace std;
int n, a, b, res;
vector<int> v[1100], u[1100];
int vis[110];
vector<pair<int, int> > x;
void dfs(int i) {
  if (vis[i] == 1) return;
  vis[i] = 1;
  for (int j = 0; j < n; j++) {
    if (x[j].first == x[i].first || x[j].second == x[i].second) dfs(j);
  }
  return;
}
int main() {
  cin >> n;
  res = 0;
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    x.push_back(make_pair(a, b));
  }
  for (int i = 0; i < n; i++) {
    if (vis[i] == 0) {
      dfs(i);
      res++;
    }
  }
  cout << (res - 1) << endl;
  return 0;
}
