#include <bits/stdc++.h>
using namespace std;
vector<int> x_[1010], y_[1010];
int ara[1010][1010] = {0};
vector<pair<int, int> > g;
void bfs(int x, int y) {
  pair<int, int> p, p1;
  int u, v, u1, v1;
  p = {x, y};
  ara[x][y] = 0;
  queue<pair<int, int> > q;
  q.push(p);
  while (!q.empty()) {
    p1 = q.front();
    q.pop();
    u = p1.first;
    v = p1.second;
    for (int i = 0; i < x_[u].size(); i++) {
      v1 = x_[u][i];
      if (ara[u][v1]) {
        ara[u][v1] = 0;
        q.push(make_pair(u, v1));
      }
    }
    for (int i = 0; i < y_[v].size(); i++) {
      u1 = y_[v][i];
      if (ara[u1][v]) {
        ara[u1][v] = 0;
        q.push(make_pair(u1, v));
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, temp, x, y;
  cin >> n;
  temp = n;
  while (temp--) {
    cin >> x >> y;
    x_[x].push_back(y);
    y_[y].push_back(x);
    ara[x][y] = 1;
    g.push_back(make_pair(x, y));
  }
  int ans = 0;
  for (int i = 0; i < g.size(); i++) {
    x = g[i].first;
    y = g[i].second;
    if (ara[x][y]) {
      ans++;
      bfs(x, y);
    }
  }
  cout << ans - 1 << endl;
  return 0;
}
