#include <bits/stdc++.h>
using namespace std;
vector<int> v[1001];
vector<set<int> > g(1001);
bool t[1001];
int p[1001];
void dfs(int i) {
  set<int>::iterator it;
  int x;
  t[i] = false;
  for (it = g[i].begin(); it != g[i].end(); it++) {
    x = (*it);
    if (t[x]) {
      p[x] = p[i];
      dfs(x);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, i, j, z, x, y, b, sum = 0;
  cin >> n;
  for (i = 1; i <= 1000; i++) {
    t[i] = true;
    p[i] = i;
  }
  int k[n + 1];
  for (i = 1; i <= n; i++) {
    cin >> x >> y;
    v[y].push_back(x);
    k[i] = x;
  }
  vector<int>::iterator it;
  vector<int>::iterator it1;
  for (i = 1; i <= 1000; i++) {
    for (it = v[i].begin(); it != v[i].end();) {
      it1 = it++;
      if (it == v[i].end())
        break;
      else {
        g[(*it1)].insert((*it));
        g[(*it)].insert((*it1));
      }
    }
  }
  for (i = 1; i <= n; i++) dfs(k[i]);
  set<int> s;
  for (i = 1; i <= n; i++) s.insert(p[k[i]]);
  sum = s.size();
  cout << sum - 1;
}
