#include <bits/stdc++.h>
using namespace std;
void count() {}
vector<int> g[101];
bool used[101];
void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < g[v].size(); ++i) {
    int to = g[v][i];
    if (!used[to]) dfs(to);
  }
}
int main() {
  int n;
  cin >> n;
  pair<int, pair<int, int> > x[101];
  pair<int, pair<int, int> > y[101];
  for (int i = 1; i <= n; ++i) {
    int l, r;
    cin >> l >> r;
    x[i] = {l, {r, i}};
    y[i] = {r, {l, i}};
  }
  sort(x + 1, x + n + 1);
  sort(y + 1, y + n + 1);
  for (int i = 2; i <= n; ++i) {
    if (x[i].first == x[i - 1].first) {
      g[x[i].second.second].push_back(x[i - 1].second.second);
      g[x[i - 1].second.second].push_back(x[i].second.second);
    }
    if (y[i].first == y[i - 1].first) {
      g[y[i].second.second].push_back(y[i - 1].second.second);
      g[y[i - 1].second.second].push_back(y[i].second.second);
    }
  }
  int ans = -1;
  for (int i = 1; i <= n; ++i) {
    if (!used[i]) {
      ans++;
      dfs(i);
    }
  }
  cout << ans << endl;
  return 0;
}
