#include <bits/stdc++.h>
using namespace std;
struct node {
  int x, y;
} arr[104];
vector<int> graph[105];
int visited[105] = {0};
void dfs(int i) {
  visited[i] = 1;
  int j = 0;
  while (j < graph[i].size()) {
    if (visited[graph[i][j]] == 0) {
      dfs(graph[i][j]);
    }
    j++;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  int x, y;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &x, &y);
    arr[i].x = x;
    arr[i].y = y;
  }
  int hash[102] = {0};
  int ct = 0;
  for (int i = 0; i < n; i++) {
    int flag = 0;
    for (int j = 0; j < n; j++) {
      if (i == j) continue;
      if (arr[j].x == arr[i].x || arr[j].y == arr[i].y) {
        graph[i].push_back(j);
        graph[j].push_back(i);
        flag = 1;
      }
    }
    if (flag == 0) visited[i] = 1;
  }
  for (int i = 0; i < n; i++) {
    if (visited[i] == 0) {
      dfs(i);
      ct++;
    }
  }
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (!graph[i].size()) c++;
  }
  printf("%d\n", c - 1 + ct);
  return 0;
}
