#include <bits/stdc++.h>
using namespace std;
vector<pair<int, int>> orig;
bool visited[1001][1001];
int curColor = 0;
bool dfs(pair<int, int>& node) {
  if (visited[node.first][node.second]) return false;
  visited[node.first][node.second] = true;
  for (auto p : orig) {
    if (p.first == node.first || p.second == node.second) {
      dfs(p);
    }
  }
  return true;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    orig.push_back({y, x});
  }
  for (int i = 0; i < n; i++) {
    if (dfs(orig[i])) {
      curColor++;
    }
  }
  cout << (curColor - 1) << endl;
  return 0;
}
