#include <bits/stdc++.h>
using namespace std;
long long arr[1002][1002];
bool visited[1002][1002];
long long dx[4] = {0, 0, 1, -1};
long long dy[4] = {1, -1, 0, 0};
void bfs(long long u, long long v) {
  arr[u][v] = -1;
  for (long long i = 1; i <= 1000; i++) {
    if (arr[u][i] == 2) {
      arr[u][i] = -1;
      bfs(u, i);
    }
    if (arr[i][v] == 2) {
      arr[i][v] = -1;
      bfs(i, v);
    }
  }
}
int main() {
  long long n;
  memset(visited, 0, sizeof visited);
  memset(arr, 0, sizeof arr);
  cin >> n;
  long long t, u, v;
  vector<pair<long long, long long> > vv;
  for (long long i = 0; i < n; i++) {
    scanf("%lld %lld", &u, &v);
    arr[u][v] = 2;
    vv.push_back(make_pair(u, v));
  }
  long long cnt = 0, cnt2 = 0;
  sort(vv.begin(), vv.end());
  for (long long i = 0; i < n; i++) {
    u = vv[i].first;
    v = vv[i].second;
    if (arr[u][v] == 2) {
      cnt++;
      bfs(u, v);
    }
  }
  cout << cnt - 1 << endl;
  return 0;
}
