#include <bits/stdc++.h>
using namespace std;
const int oo = (int)1e9;
const double PI = 2 * acos(0.0);
const double eps = 1e-7;
const int MAXN = 1e5 + 10;
int dx[] = {1, 0, 0, -1, -1, -1, 1, 1};
int dy[] = {0, 1, -1, 0, 1, -1, 1, -1};
char dir[4] = {'R', 'U', 'D', 'L'};
bool vis[509];
int n, a[101], b[101];
void dfs(int idx) {
  if (vis[idx]) return;
  vis[idx] = 1;
  for (int i = 0; i < n; i++)
    if (vis[i] == 0 && (a[idx] == a[i] || b[idx] == b[i])) dfs(i);
}
int main() {
  int cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i] >> b[i];
  for (int i = 0; i < n; i++)
    if (vis[i] == 0) cnt++, dfs(i);
  cout << cnt - 1 << endl;
}
