#include <bits/stdc++.h>
using namespace std;
inline void FAST_IO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
template <class F, class S>
ostream& operator<<(ostream& os, pair<F, S>& p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, vector<T>& v) {
  os << "(";
  if (v.size()) os << v[0];
  for (int i = 1; i < v.size(); ++i) os << "," << v[i];
  os << ")";
  return os;
}
int main() {
  FAST_IO();
  int n;
  cin >> n;
  struct Point {
    int x, y;
  };
  vector<Point> p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i].x >> p[i].y;
  }
  vector<vector<int>> g(n);
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j < n; ++j) {
      if (p[i].x == p[j].x || p[i].y == p[j].y) {
        g[i].push_back(j);
        g[j].push_back(i);
      }
    }
  }
  vector<int> visited(n);
  function<void(int)> dfs = [&](int v) {
    visited[v] = true;
    for (auto adj : g[v]) {
      if (!visited[adj]) dfs(adj);
    }
  };
  int resp = 0;
  for (int i = 0; i < n; ++i) {
    if (!visited[i]) {
      dfs(i);
      resp++;
    }
  }
  cout << resp - 1 << endl;
  return 0;
}
