#include <bits/stdc++.h>
using namespace std;
using lli = long long int;
pair<int, int> nodes[100];
vector<int> v[100];
bool visited[100] = {0};
int dfs(int r) {
  visited[r] = true;
  for (int i = 0; i < v[r].size(); ++i) {
    if (!visited[v[r][i]]) {
      dfs(v[r][i]);
    }
  }
  return 0;
}
int main() {
  int n;
  cin >> n;
  int x, y;
  for (int i = 0; i < n; ++i) {
    cin >> x >> y;
    nodes[i].first = x;
    nodes[i].second = y;
    for (int j = 0; j < i; ++j) {
      if (nodes[i].first == nodes[j].first ||
          nodes[i].second == nodes[j].second) {
        v[i].push_back(j);
        v[j].push_back(i);
      }
    }
  }
  int ans = -1;
  for (int i = 0; i < n; ++i) {
    if (!visited[i]) {
      ans++;
      dfs(i);
    }
  }
  cout << ans;
  return 0;
}
