#include <bits/stdc++.h>
using namespace std;
const long long Mod = 1000000007;
const long long INF = 999999999999999999;
const long long NN = (long long)(1e2 + 5);
int mod(string s, int a) {
  int ans = 0;
  for (int i = 0; i < s.length(); i++) ans = (ans * 10 + (int)s[i] - '0') % a;
  return ans;
}
long long min(long long x, long long y) {
  if (x < y) return x;
  return y;
}
long long max(long long x, long long y) {
  if (x > y) return x;
  return y;
}
long long power(long long x, unsigned long long y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
long long hcf(long long a, long long b) {
  if (b == 0) return a;
  return hcf(b, a % b);
}
long long n;
vector<pair<long long, long long>> v(NN);
vector<long long> vis(NN);
void dfs(long long node) {
  vis[node]++;
  auto p = v[node];
  long long i;
  for (i = 1; i <= n; i++) {
    if (vis[i]) continue;
    if (p.first == v[i].first || p.second == v[i].second) dfs(i);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long TT = 1;
  while (TT--) {
    cin >> n;
    long long i, ans = 0;
    for (i = 1; i <= n; i++) cin >> v[i].first >> v[i].second;
    for (i = 1; i <= n; i++)
      if (!vis[i]) {
        dfs(i);
        ans++;
      }
    cout << ans - 1 << "\n";
  }
}
