#include <bits/stdc++.h>
using namespace std;
inline long long input() {
  long long n;
  cin >> n;
  return n;
}
long long pw(long long a, long long b) {
  return (!b ? 1 : (b & 1 ? a * pw(a * a, b / 2) : pw(a * a, b / 2)));
}
const int maxn = 1e2 + 10, mod = 1e9 + 7;
bool check[maxn];
int n, m;
vector<int> adj[maxn];
vector<int> saf;
void DFS(int i) {
  check[i] = true;
  for (auto u : adj[i]) {
    if (check[u]) continue;
    DFS(u);
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  vector<pair<int, int> > inp;
  inp.push_back({0, 0});
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    inp.push_back({x, y});
  }
  for (int i = 1; i < inp.size(); i++) {
    for (int j = 1; j < inp.size(); j++) {
      if (i == j) continue;
      if (inp[i].first == inp[j].first || inp[i].second == inp[j].second) {
        adj[i].push_back(j);
        adj[j].push_back(i);
      }
    }
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    if (check[i]) continue;
    DFS(i);
    ans++;
  }
  cout << ans;
  return 0;
}
