#include <bits/stdc++.h>
const double pi = acos(-1.0);
using namespace std;
inline long long MAX2(long long a, long long b) {
  return (a) > (b) ? (a) : (b);
}
inline long long MAX3(long long a, long long b, long long c) {
  return (a) > (b) ? ((a) > (c) ? (a) : (c)) : ((b) > (c) ? (b) : (c));
}
inline long long MIN2(long long a, long long b) {
  return (a) < (b) ? (a) : (b);
}
inline long long MIN3(long long a, long long b, long long c) {
  return (a) < (b) ? ((a) < (c) ? (a) : (c)) : ((b) < (c) ? (b) : (c));
}
int mod = 1e9 + 7;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long powmod(long long a, long long b) {
  long long res = 1;
  if (a >= mod) a %= mod;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a;
    if (res >= mod) res %= mod;
    a = a * a;
    if (a >= mod) a %= mod;
  }
  return res;
}
unsigned long int setbits(long long n) {
  unsigned long int count = 0;
  while (n) {
    n &= (n - 1);
    count++;
  }
  return count;
}
int modInverse(int A, int M) { return powmod(A, M - 2); }
vector<int> adj[105];
bool vis[105];
void dfs(int src) {
  vis[src] = true;
  for (auto child : adj[src]) {
    if (vis[child] == false) dfs(child);
  }
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int> > e;
  for (int i = 0; i < n; i++) {
    int second, en;
    cin >> second >> en;
    e.push_back(make_pair(second, en));
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (e[i].first == e[j].first || e[i].second == e[j].second) {
        adj[i].push_back(j);
        adj[j].push_back(i);
      }
    }
  }
  int comp = 0;
  for (int i = 0; i < n; i++) {
    if (vis[i] == false) {
      dfs(i);
      comp++;
    }
  }
  cout << comp - 1 << '\n';
  return 0;
}
