#include <bits/stdc++.h>
using namespace std;
bool grid[1002][1002];
bool check[1002][1002];
class Point {
 public:
  int x, y;
  Point(int x, int y) {
    this->x = x;
    this->y = y;
  }
};
void dfs(int x, int y) {
  check[x][y] = true;
  for (int i = 0; i < 1002; i++) {
    if (!check[i][y] && grid[i][y]) dfs(i, y);
  }
  for (int j = 0; j < 1002; j++) {
    if (!check[x][j] && grid[x][j]) dfs(x, j);
  }
}
int nou(vector<Point> &pts) {
  int tps = 0;
  for (int i = 0; i < pts.size(); i++) {
    int x = pts[i].x;
    int y = pts[i].y;
    if (!check[x][y]) {
      tps++;
      dfs(x, y);
    }
  }
  return tps - 1;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < 1002; i++) {
    for (int j = 0; j < 1002; j++) grid[i][j] = false;
  }
  vector<Point> pts;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    grid[x][y] = true;
    pts.push_back(Point(x, y));
  }
  for (int i = 0; i < 1002; i++) {
    for (int j = 0; j < 1002; j++) check[i][j] = false;
  }
  cout << nou(pts) << '\n';
}
