#include <bits/stdc++.h>
const long long MOD = (long long)1e9 + 7;
using namespace std;
long long power(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2 == 0) return 1LL * power(a, b / 2) * power(a, b / 2);
  b--;
  return 1LL * power(a, b) * a;
}
bool visR[1001];
bool visC[1001];
map<pair<int, int>, bool> vis;
map<pair<int, int>, bool> e;
vector<pair<int, int>> s;
int main() {
  int n, x, y;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x, &y);
    e[make_pair(x, y)] = true;
    s.push_back(make_pair(x, y));
  }
  int ans = -1;
  sort(s.begin(), s.end());
  stack<pair<int, int>> st;
  while (s.empty() == false) {
    stack<pair<int, int>> st2;
    if (st.empty()) {
      st.push(s.back());
      if (!vis[s.back()]) ans++;
      vis[s.back()] = true;
      visC[s.back().first] = visR[s.back().second] = true;
      s.pop_back();
    }
    for (int x = 1; x <= 1000; x++) {
      if (e[make_pair(x, st.top().second)]) {
        if (!vis[make_pair(x, st.top().second)])
          st2.push(make_pair(x, st.top().second));
        vis[make_pair(x, st.top().second)] = true;
        visC[x] = true;
      }
      if (e[make_pair(st.top().first, x)]) {
        if (!vis[make_pair(st.top().first, x)])
          st2.push(make_pair(st.top().first, x));
        vis[make_pair(st.top().first, x)] = true;
        visR[x] = true;
      }
    }
    st.pop();
    while (st2.empty() == false) {
      st.push(st2.top());
      st2.pop();
    }
  }
  printf("%d\n", ans);
}
