#include <bits/stdc++.h>
using namespace std;

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

typedef long long LL;

int uf[40001];

int find(int n){
  if(uf[n]==n) return n;
  else return uf[n]=find(uf[n]);
}

void uni(int m,int n){
  uf[find(m)]=find(n);
  return;
}

int main(){
  int n,w,h;
  cin >> n >> w >> h;
  map<int,int> xset,yset;
  bool kabeflag=false;
  for(int i=0;i<=n;i++){
    uf[i]=i;
  }
  int x,y;
  for(int i=1;i<=n;i++){
    cin >> x >> y;
    if(x==1||x==w||y==1||y==h){
      kabeflag=true;
    }
    if(xset[x]){
      uni(i,xset[x]);
    }else{
      xset[x]=i;
    }
    if(yset[y]){
      uni(i,yset[y]);
    }else{
      yset[y]=i;
    }
  }
  vector<int> isroot(n+1,0);
  for(int i=1;i<=n;i++){
    isroot[find(i)]=1;
  }
  int count=0;
  vector<int> hasi(4,0);
  for(int i=1;i<=n;i++){
    count+=isroot[i];
  }
  if(count==1){
    cout << n-1 << endl;
  }else{
    if(kabeflag){
      cout << n-1+count-1 << endl;
    }else{
      cout << n-1+count << endl;
    }
  }
  return 0;
}

