#include <bits/stdc++.h>
using namespace std;
int lin[1010], col[1010];
int sef[110];
int find(int s);
void unire(int l, int c);
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) sef[i] = i;
  int x, y;
  for (int i = 1; i <= n; i++) {
    cin >> x >> y;
    if (lin[x] == 0) lin[x] = i;
    if (col[y] == 0) col[y] = i;
    unire(x, y);
    sef[i] = find(lin[x]);
  }
  set<int> s;
  for (int i = 1; i <= n; i++) s.insert(find(i));
  cout << s.size() - 1;
  return 0;
}
int find(int s) {
  if (s != sef[s]) s = find(sef[s]);
  return s;
}
void unire(int l, int c) {
  int a = lin[l];
  int b = col[c];
  a = find(a);
  b = find(b);
  sef[a] = b;
}
