#include <bits/stdc++.h>
using namespace std;
class Point {
 public:
  int x, y;
  bool visited;
  vector<Point*> adj;
  void set(int, int);
  void addNeighbour(Point*);
  void dfs();
};
void Point::set(int a, int b) {
  x = a;
  y = b;
  visited = false;
}
void Point::addNeighbour(Point* P) { adj.push_back(P); }
void Point::dfs() {
  if (visited == false) {
    visited = true;
    for (int i = 0; i < adj.size(); i++) {
      if (adj[i]->visited == 0) {
        adj[i]->dfs();
      }
    }
  }
}
int main() {
  int n, x, y;
  cin >> n;
  Point P[n];
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    P[i].set(x, y);
  }
  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) {
        P[i].addNeighbour(&P[j]);
        P[j].addNeighbour(&P[i]);
      }
    }
  }
  int k = 0;
  for (int i = 0; i < n; i++) {
    if (P[i].visited == false) {
      P[i].dfs();
      k++;
    }
  }
  cout << k - 1 << endl;
  return 0;
}
