#include <bits/stdc++.h>
using namespace std;
long long int binpow(long long int a, long long int b) {
  if (b == 0) return 1;
  long long int res = binpow(a, b / 2);
  if (b % 2)
    return res * res * a;
  else
    return res * res;
}
long long int modexpo(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
vector<int> getPrimes() {
  int M = 10000005;
  vector<int> p(M, 1), prime;
  for (int i = 2; i * i <= M; ++i) {
    if (p[i]) {
      for (int j = i * i; j <= M; j += i) p[i] = 0;
    }
  }
  for (int i = 2; i < M; ++i)
    if (p[i]) prime.push_back(i);
  return prime;
}
const int MAX = (int)1e3 + 5;
vector<vector<int>> adj(MAX);
bool cmp(pair<int, int> a, pair<int, int> b) {
  return a.first > b.first && a.second < b.second;
}
int bfs(vector<pair<int, int>> &pts, vector<vector<int>> &row,
        vector<vector<int>> &col) {
  vector<vector<int>> used(1005, vector<int>(1005, 0));
  int cnt = 0;
  for (auto p : pts) {
    if (used[p.first][p.second] == 0) {
      cnt++;
      queue<pair<int, int>> q;
      q.push(p);
      used[p.first][p.second] = 1;
      while (!q.empty()) {
        int a = q.front().first, b = q.front().second;
        q.pop();
        for (int i = 0; i < (int)row[a].size(); ++i) {
          if (used[a][row[a][i]] == 0) {
            q.push({a, row[a][i]});
            used[a][row[a][i]] = 1;
          }
        }
        for (int i = 0; i < (int)col[b].size(); ++i) {
          if (used[col[b][i]][b] == 0) {
            q.push({col[b][i], b});
            used[col[b][i]][b] = 1;
          }
        }
      }
    }
  }
  return cnt - 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    int n;
    cin >> n;
    vector<vector<int>> x(1005), y(1005);
    vector<pair<int, int>> pts;
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      pts.push_back({a, b});
      x[a].push_back(b);
      y[b].push_back(a);
    }
    cout << bfs(pts, x, y) << "\n";
  }
  return 0;
}
