#include <bits/stdc++.h>
using namespace std;
class Timer {
  clock_t start;
  string name;

 public:
  Timer() {
    name = "";
    start = clock();
  }
  Timer(string s) {
    name = s;
    start = clock();
  }
  ~Timer() {
    fprintf(stderr, "%s: %.3gs\n", name.c_str(),
            1.0 * (clock() - start) / CLOCKS_PER_SEC);
  }
};
const double EPS = 1e-9;
const double PI = acos(-1.0);
inline bool isEq(double a, double b) { return abs(a - b) < EPS; }
inline bool isEq(double a, double b, double eps) { return abs(a - b) < eps; }
inline double toRad(double deg) { return deg * PI / 180; }
inline double toDeg(double rad) { return rad * 180 / PI; }
const int INF = 0x3fffffff;
const int MAX = 2010;
const int MOD = 1000000007;
const int precision = 10;
int relations[MAX];
int findRep(int n) {
  if (n == relations[n])
    return n;
  else
    return relations[n] = findRep(relations[n]);
}
bool make_union(int a, int b) {
  int x = findRep(a);
  int y = findRep(b);
  if (x != y) {
    relations[x] = y;
    return 1;
  }
  return 0;
}
vector<pair<int, int> > ps;
int main(int argc, char* argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, x, y;
  cin >> n;
  for (int i = 0; i < MAX; i++) relations[i] = i;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    for (int j = 0; j < i; j++) {
      if (x == ps[j].first || y == ps[j].second) make_union(i, j);
    }
    ps.push_back({x, y});
  }
  pair<int, int> a, b;
  int ans = 0;
  for (int i = 0; i < ps.size(); i++) {
    for (int j = i + 1; j < ps.size(); j++) {
      ans += make_union(i, j);
    }
  }
  cout << ans << endl;
  return 0;
}
