#include <bits/stdc++.h>
using namespace std;
int vis[1001][1001] = {0};
vector<pair<int, int> > v;
void dfs(int x, int y) {
  vis[x][y] = 1;
  int i = x, j = y;
  for (i = x + 1; i != x; i++) {
    if (i == 1001) {
      i = -1;
      continue;
    }
    if (count(v.begin(), v.end(), make_pair(i, j)) != 0 && vis[i][j] == 0) {
      dfs(i, j);
    }
  }
  for (j = y + 1; j != y; j++) {
    if (j == 1001) {
      j = -1;
      continue;
    }
    if (count(v.begin(), v.end(), make_pair(i, j)) != 0 && vis[i][j] == 0) {
      dfs(i, j);
    }
  }
}
int main() {
  std::ios_base::sync_with_stdio(false);
  int n, i;
  cin >> n;
  int x, y, c = 0;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }
  vector<pair<int, int> >::iterator it;
  for (it = v.begin(); it != v.end(); it++) {
    if (vis[(*it).first][(*it).second] == 0) {
      dfs((*it).first, (*it).second);
      c++;
    }
  }
  cout << c - 1;
}
