#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> hor[1001];
vector<int> ver[1001];
bool vis[1001][1001];
int nodes = 0;
int main() {
  cin >> n;
  queue<pair<int, int> > mQ;
  queue<pair<int, int> > q;
  memset(vis, 0, sizeof(vis));
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    mQ.push(make_pair(x, y));
    hor[x].push_back(y);
    ver[y].push_back(x);
  }
  while (!mQ.empty()) {
    pair<int, int> curr = mQ.front();
    mQ.pop();
    if (vis[curr.first][curr.second]) {
      continue;
    } else {
      q.push(curr);
      ++nodes;
    }
    while (!q.empty()) {
      int x = q.front().first;
      int y = q.front().second;
      q.pop();
      vis[x][y] = 1;
      for (int i = 0; i < hor[x].size(); ++i) {
        if (!vis[x][hor[x][i]]) {
          q.push(make_pair(x, hor[x][i]));
        }
      }
      for (int i = 0; i < ver[y].size(); ++i) {
        if (!vis[ver[y][i]][y]) {
          q.push(make_pair(ver[y][i], y));
        }
      }
    }
  }
  cout << nodes - 1;
  return 0;
}
