#include <bits/stdc++.h>

using namespace std;

struct UnionFind
{
  vector< int > data;

  UnionFind(int sz)
  {
    data.assign(sz, -1);
  }

  void unite(int x, int y)
  {
    x = find(x), y = find(y);
    if(x != y) {
      if(data[x] > data[y]) swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
  }

  int find(int k)
  {
    if(data[k] < 0) return (k);
    return (data[k] = find(data[k]));
  }

  int size(int k)
  {
    return (-data[find(k)]);
  }
};

int main()
{
  int N, W, H;
  vector< int > xs[100000], ys[100000];

  scanf("%d %d %d", &N, &W, &H);
  bool ei = false;
  for(int i = 0; i < N; i++) {
    int X, Y;
    scanf("%d %d", &X, &Y);
    xs[--X].push_back(i);
    ys[--Y].push_back(i);
    ei |= X == 0 || Y == 0 || X == W - 1 || Y == H - 1;
  }

  UnionFind tree(N);
  for(int i = 0; i < 100000; i++) {
    for(int j = 1; j < xs[i].size(); j++) tree.unite(xs[i][j - 1], xs[i][j]);
    for(int j = 1; j < ys[i].size(); j++) tree.unite(ys[i][j - 1], ys[i][j]);
  }

  int cnt = 0;
  for(int i = 0; i < N; i++) cnt += tree.find(i) == i;
  int ret = N - cnt;
  if(cnt > 1) ret += cnt * 2 - 1 - ei;
  printf("%d\n", ret);
}