#include <bits/stdc++.h>
using namespace std;
pair<int, int> points[101];
int n;
bool v[101];
int diff(pair<int, int>& a, pair<int, int>& b) {
  int cnt = 0;
  if (a.first == b.first) cnt++;
  if (a.second == b.second) cnt++;
  return cnt;
}
void f(int i) {
  for (int j = 0; j < n; j++)
    if (!v[j] && diff(points[i], points[j])) {
      v[j] = true;
      f(j);
    }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> points[i].first >> points[i].second;
    v[i] = false;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++)
    if (!v[i]) {
      v[i] = true;
      cnt++;
      f(i);
    }
  cout << cnt - 1 << '\n';
  return 0;
}
