#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <set>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n, w, h;
vector<int> xIndex[100010];
vector<int> yIndex[100010];
int xs[100010];
int ys[100010];
bool visit[100010];

int main() {
  while (scanf("%d %d %d", &n, &w, &h) > 0) {
    MEMSET(visit, false);
    REP(i, 100010) { xIndex[i].clear(); yIndex[i].clear(); }
    int edge = 0;
    int ans = n - 2;
    REP(i, n) {
      int x, y;
      scanf("%d %d", &x, &y);
      if (x == 1 || x == w || y == 1 || y == h) { edge = 1; }
      x--; y--;
      xs[i] = x; ys[i] = y;
      xIndex[x].push_back(i);
      yIndex[y].push_back(i);
    }
    REP(i, n) {
      if (visit[i]) { continue; }
      visit[i] = true;
      ans++;
      queue<int> que;
      que.push(i);
      while (!que.empty()) {
        int index = que.front();
        que.pop();
        FORIT(it, xIndex[xs[index]]) {
          if (visit[*it]) { continue; }
          visit[*it] = true;
          que.push(*it);
        }
        FORIT(it, yIndex[ys[index]]) {
          if (visit[*it]) { continue; }
          visit[*it] = true;
          que.push(*it);
        }
      }
    }
    if (ans != n - 1 && !edge) { ans++; }
    printf("%d\n", ans);
  }
}