#include <bits/stdc++.h>
using namespace std;
const int MAXM = 100000 + 10;
vector<int> G[MAXM];
bool M[MAXM], f = 1;
bool isc(int x1, int y1, int x2, int y2) {
  if (abs(x1) == abs(x2) || abs(y1) == abs(y2))
    return 1;
  else
    return 0;
}
void DFS(int k) {
  M[k] = 1;
  for (int i = 0; i < (int)G[k].size(); i++)
    if (!M[G[k][i]]) DFS(G[k][i]);
}
int main() {
  int n, P[MAXM][2], ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> P[i][0] >> P[i][1];
  for (int i = 1; i <= n; i++)
    for (int j = i + 1; j <= n; j++)
      if (isc(P[i][0], P[i][1], P[j][0], P[j][1])) {
        G[i].push_back(j);
        G[j].push_back(i);
      }
  while (f) {
    f = 0;
    for (int i = 1; i <= n; i++) M[i] = 0;
    DFS(1);
    for (int i = 1; i <= n; i++)
      if (!M[i]) {
        f = 1;
        ans++;
        G[1].push_back(i);
        G[i].push_back(1);
        break;
      }
  }
  cout << ans;
  cin >> n;
  return 0;
}
