#include <bits/stdc++.h>
using namespace std;
pair<int, int> a[110];
int pos[100];
vector<int> s[110];
int n;
int loc;
void un(int x, int y) {
  if (s[pos[x]].size() > s[pos[y]].size()) swap(x, y);
  int now, px, py, ss;
  px = pos[x];
  py = pos[y];
  ss = s[px].size();
  int i, j;
  for (i = 0; i < ss; ++i) {
    now = s[px][i];
    pos[now] = py;
    s[py].push_back(now);
  }
  s[px].clear();
  loc--;
  if (px != loc) {
    ss = s[loc].size();
    for (i = 0; i < ss; ++i) {
      now = s[loc][i];
      pos[now] = px;
    }
    s[px] = s[loc];
    s[loc].clear();
  }
  return;
}
int main() {
  int x, y;
  cin >> n;
  loc = n;
  int i, j;
  for (i = 0; i < n; ++i) {
    pos[i] = i;
    cin >> a[i].first >> a[i].second;
    s[i].push_back(i);
  }
  for (i = 0; i < n; ++i) {
    for (j = 0; j < n; ++j) {
      if (pos[i] != pos[j]) {
        if (a[i].first == a[j].first || a[i].second == a[j].second) {
          un(i, j);
        }
      }
    }
  }
  cout << loc - 1 << endl;
  return 0;
}
