#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, MOD = 1e9 + 7;
int a, b, i, j, n, m, k, num, vis[5000];
map<int, vector<int> > v;
void dfs(int i) {
  vis[i] = 1;
  for (int k : v[i])
    if (!vis[k]) dfs(k);
}
int main() {
  cin >> n;
  while (n--) {
    cin >> a >> b;
    v[a].push_back(b + 1500);
    v[b + 1500].push_back(a);
  }
  for (auto x : v) {
    if (!vis[x.first]) {
      num++;
      dfs(x.first);
    }
  }
  cout << num - 1;
  return 0;
}
