#include <bits/stdc++.h>
using namespace std;
int father[105];
struct node {
  int id, x, y;
} s[105];
set<int> se;
int get(int v) {
  if (father[v] != v) father[v] = get(father[v]);
  return father[v];
}
int main() {
  int n, i, j, k;
  while (scanf("%d", &n) != EOF) {
    for (i = 1; i <= n; i++) {
      scanf("%d %d", &s[i].x, &s[i].y);
      s[i].id = i;
      father[i] = i;
    }
    for (i = 1; i <= n; i++)
      for (j = i + 1; j <= n; j++) {
        int x = get(s[i].id);
        int y = get(s[j].id);
        if (x == y)
          continue;
        else if (s[i].x == s[j].x || s[i].y == s[j].y) {
          father[x] = y;
        }
      }
    se.clear();
    for (i = 1; i <= n; i++) se.insert(get(i));
    printf("%d\n", se.size() - 1);
  }
}
