#include <bits/stdc++.h>
using namespace std;
class UnionFind {
 public:
  int n;
  vector<int> D;
  UnionFind(int n) : n(n) {
    D = vector<int>(n);
    for (int i = 0; i < n; ++i) D[i] = -1;
  }
  UnionFind() : n(0) {}
  void merge(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) D[y] = x;
  }
  bool same(int x, int y) { return get(x) == get(y); }
  int get(int x) { return D[x] == -1 ? x : get(D[x]); }
};
const int SIZE = 101;
int n;
int X[SIZE];
int Y[SIZE];
void input() {
  for (int i = 0; i < n; ++i) cin >> X[i] >> Y[i];
}
bool checkSameLine(int a, int b) { return X[a] == X[b] || Y[a] == Y[b]; }
int solve() {
  UnionFind uf(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (checkSameLine(i, j)) {
        uf.merge(i, j);
      }
    }
  }
  int res = 0;
  for (int i = 0; i < n; ++i) {
    if (uf.D[i] == -1) res++;
  }
  return res - 1;
}
int main() {
  while (cin >> n) {
    input();
    cout << solve() << endl;
  }
  return 0;
}
