#include <bits/stdc++.h>
using namespace std;
void Yahia74() {
  cout << fixed << setprecision(8);
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
}
vector<vector<int>> vecX(1001), vecY(1001);
vector<pair<int, int>> pts;
bool visited[1001][1001] = {};
int cnt = 0;
void dfs(int x, int y) {
  if (visited[x][y]) return;
  visited[x][y] = 1;
  for (int i = 0; i < ((int)(vecX[x]).size()); ++i) {
    if (!visited[x][vecX[x][i]]) dfs(x, vecX[x][i]);
  }
  for (int i = 0; i < ((int)(vecY[y]).size()); ++i) {
    if (!visited[vecY[y][i]][y]) dfs(vecY[y][i], y);
  }
}
int main() {
  Yahia74();
  int n, a, b;
  memset(visited, -1, sizeof(visited));
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a >> b;
    vecX[a].push_back(b);
    vecY[b].push_back(a);
    visited[a][b] = 0;
    pts.push_back({a, b});
  }
  for (int i = 0; i < n; ++i) {
    if (!visited[pts[i].first][pts[i].second])
      dfs(pts[i].first, pts[i].second), ++cnt;
  }
  cout << cnt - 1;
  return 0;
}
