#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> v[2][100000];
int n, w, h, x, y, p[40000], r[40000], f, ans;

int find(int x){
  if(p[x] == x) return x;
  else return p[x] = find(p[x]);
}

bool unite(int x, int y){
  x = find(x);
  y = find(y);
  if(x == y) return false;
  if(r[x] < r[y]) p[x] = y;
  else{
    p[y] = x;
    if(r[x] == r[y]) r[x]++;
  }
  return true;
}

int main(){
  cin >> n >> w >> h;
  for(int i=0;i<n;i++){
    cin >> x >> y;
    x--; y--;
    if(min(x, y) == 0 || x == w - 1 || y == h - 1) f = 1;
    v[0][y].push_back(i);
    v[1][x].push_back(i);
  }
  for(int i=0;i<n;i++){
    p[i] = i;
    r[i] = 0;
  }
  for(int i=0;i<h;i++) for(int j=1;j<v[0][i].size();j++) if(unite(v[0][i][j-1], v[0][i][j])) ans++;
  for(int i=0;i<w;i++) for(int j=1;j<v[1][i].size();j++) if(unite(v[1][i][j-1], v[1][i][j])) ans++;
  if(n - ans != 1) ans += (n - ans) * 2 - 1 - f;
  cout << ans << endl;
}