#include <bits/stdc++.h>
using namespace std;
class v {
 public:
  int x, y;
};
vector<v> point;
bool visit[101];
int number_of_vertex;
void input() {
  cin >> number_of_vertex;
  v ver;
  for (int i = 1; i <= number_of_vertex; i++) {
    cin >> ver.x >> ver.y;
    point.push_back(ver);
  }
}
void dfs(int a) {
  visit[a] = true;
  for (int i = 0; i < point.size(); i++) {
    if ((point[a].x == point[i].x || point[a].y == point[i].y) && !visit[i]) {
      dfs(i);
    }
  }
}
void do_job() {
  int tree = 0;
  for (int i = 0; i < point.size(); i++) {
    if (!visit[i]) {
      tree++;
      dfs(i);
    }
  }
  cout << --tree;
}
int main() {
  input();
  do_job();
}
