#include <bits/stdc++.h>
using namespace std;
int n;
bool vis[110];
vector<pair<int, int> > drifts;
void dfs(int idx) {
  vis[idx] = true;
  pair<int, int> cur = drifts[idx];
  for (long long i = 0; i < (n); ++i)
    if (!vis[i] &&
        (drifts[i].first == cur.first || drifts[i].second == cur.second))
      dfs(i);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (long long i = 0; i < (n); ++i) {
    pair<int, int> temp;
    cin >> temp.first >> temp.second;
    drifts.push_back(temp);
  }
  int cnt = 0;
  for (long long i = 0; i < (n); ++i)
    if (!vis[i]) {
      dfs(i);
      ++cnt;
    }
  cout << cnt - 1 << '\n';
  return 0;
}
