#include <bits/stdc++.h>
using namespace std;
vector<int> arr[2002];
set<int> st;
bool vist[2002];
void dfs(int node) {
  vist[node] = 1;
  for (int i = 0; i < (int)arr[node].size(); ++i) {
    if (!vist[arr[node][i]]) dfs(arr[node][i]);
  }
}
int main() {
  int n;
  cin >> n;
  int x, y;
  for (int i = 0; i < n; ++i) {
    cin >> x >> y;
    arr[x].push_back(y + 1000);
    arr[y + 1000].push_back(x);
    st.insert(x);
  }
  int ans = -1;
  for (auto i = st.begin(); i != st.end(); ++i) {
    if (!vist[*i]) {
      ans++;
      dfs(*i);
    }
  }
  cout << ans << endl;
}
