#include <bits/stdc++.h>
using namespace std;
void dfs(int cur, int n, vector<vector<int>>& mat, vector<int>& vis) {
  vis[cur] += 1;
  if (vis[cur] == 2) {
    printf("%d ", cur);
    return;
  }
  for (int i = 1; i <= n; ++i) {
    if ((vis[i] < 2) && mat[cur][i] == 1) {
      dfs(i, n, mat, vis);
    }
  }
}
void dfs_graph(int cur, int n, vector<pair<int, int>>& coords,
               vector<int>& groups) {
  groups[cur] = 1;
  int curr_x = coords[cur].first;
  int curr_y = coords[cur].second;
  for (int i = 0; i < n; ++i) {
    int next_x = coords[i].first;
    int next_y = coords[i].second;
    if (groups[i] != 1 && (next_x == curr_x || next_y == curr_y)) {
      dfs_graph(i, n, coords, groups);
    }
  }
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> coords;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    coords.push_back({x, y});
  }
  int count_groups = 0;
  vector<int> groups(n);
  for (int i = 0; i < n; ++i) {
    if (groups[i] != 1) {
      dfs_graph(i, n, coords, groups);
      count_groups++;
    }
  }
  cout << (count_groups - 1) << "\n";
  return 0;
}
