#include <bits/stdc++.h>
using namespace std;
vector<int> graph[105];
int parent[105];
int sz[105];
int findParent(int v) {
  if (parent[v] != v) {
    parent[v] = findParent(parent[v]);
  }
  return parent[v];
}
void unn(int x, int y) {
  int xroot = findParent(x);
  int yroot = findParent(y);
  if (xroot == yroot) {
    return;
  }
  if (sz[xroot] >= sz[yroot]) {
    sz[xroot] += sz[yroot];
    parent[yroot] = xroot;
  } else {
    sz[yroot] += sz[xroot];
    parent[xroot] = yroot;
  }
}
pair<int, int> a[105];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i].first >> a[i].second;
    sz[i] = 1;
    parent[i] = i;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j) {
        continue;
      }
      if (a[i].first == a[j].first) {
        graph[i].push_back(j);
      }
      if (a[i].second == a[j].second) {
        graph[i].push_back(j);
      }
    }
  }
  for (int v = 0; v < n; ++v) {
    for (auto u : graph[v]) {
      unn(v, u);
    }
  }
  int res = 0;
  for (int v = 0; v < n; ++v) {
    for (int u = 0; u < n; ++u) {
      if (u == v) {
        continue;
      }
      int vp = findParent(v);
      int up = findParent(u);
      if (vp != up) {
        res++;
        unn(v, u);
      }
    }
  }
  cout << res << "\n";
  return 0;
}
