#include <bits/stdc++.h>
#pragma optimize("", off)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/STACK:36777216")
const int mod = 1000000007;
using namespace std;
template <class T>
void smin(T& a, T val) {
  if (a > val) a = val;
}
template <class T>
void smax(T& a, T val) {
  if (a < val) a = val;
}
template <typename T>
inline std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
  bool first = true;
  os << "[";
  for (auto i : v) {
    if (!first) os << ", ";
    os << i;
    first = false;
  }
  return os << "]";
}
const int N = 1e3 + 10;
int n, cnt;
std::vector<pair<int, int> > v;
bool used[N];
void dfs(int x) {
  used[x] = 1;
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      if (v[x].first == v[i].first or v[x].second == v[i].second) {
        dfs(i);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; ++i) {
    int x, y;
    cin >> x >> y;
    v.push_back({x, y});
  }
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      dfs(i);
      cnt++;
    }
  }
  cout << max(cnt - 1, 0);
  return 0;
}
