#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define chmin(a,b) (a)=min((a),(b));
#define chmax(a,b) (a)=max((a),(b));
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) cerr<<(#v)<<":";for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;
#define printVS(vs) cerr<<(#vs)<<":"<<endl;for(auto(s):(vs)){cerr<<(s)<< endl;}
#define printVV(vv) cerr<<(#vv)<<":"<<endl;for(auto(v):(vv)){for(auto(x):(v)){cerr<<" "<<(x);}cerr<<endl;}
#define printP(p) cerr<<(#p)<<(p).first<<" "<<(p).second<<endl;
#define printVP(vp) cerr<<(#vp)<<":"<<endl;for(auto(p):(vp)){cerr<<(p).first<<" "<<(p).second<<endl;}

inline void output(){ cerr << endl; }
template<typename First, typename... Rest>
inline void output(const First& first, const Rest&... rest) {
    cerr << first << " "; output(rest...);
}

using ll = long long;
using Pii = pair<int, int>;
using TUPLE = tuple<int, int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
const int inf = 1ll << 60;
const int mod = 1e9 + 7;
using Graph = vector<vector<int>>;

class UnionFind {
private:
    const int n;
    vector<int> uni;
public:
    UnionFind(int _n) : n(_n), uni(_n, -1) {}
    int root(int x) {
        if (uni[x] < 0) return x;
        return uni[x] = root(uni[x]);
    }
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (uni[x] > uni[y]) swap(x, y);
        uni[x] += uni[y];
        uni[y] = x;
        return true;
    }
    int getSize(int x) {
        return -uni[root(x)];
    }
    int numOfGroups() {
        int ret = 0;
        rep(i, n) {
            if (uni[i] < 0) ret++;
        }
        return ret;
    }
    void print() {
        for (auto x : uni) cout << x << " ";
        cout << endl;
    }
};

struct Range {
    int l, r; // [l, r]
    Range(){}
    Range(int _l, int _r) : l(_l), r(_r) {}
    bool operator<( const Range& o ) const {
        if (l != o.l) return l < o.l;
        return r < o.r;
    }
    void print() {
        cout << "l = " << l << ", r = " << r << endl;
    }
};

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    int N, H, W;
    cin >> N >> H >> W;
    vector<Pii> vp;
    map<Pii, int> mp;
    rep(i, N) {
        int r, c;
        cin >> r >> c;
        r--; c--;
        vp.emplace_back(r, c);
        mp[Pii(r, c)] = i;
    }

    UnionFind uf(N);
    sort(all(vp));
    rep(i, N) {
        if (vp[i].first == vp[i + 1].first) {
            uf.unite(mp[vp[i]], mp[vp[i + 1]]);
        }
    }

    sort(all(vp), [](const Pii& p, const Pii& q) {
        if (p.second != q.second) return p.second < q.second;
        return p.first < q.first;
    });
    rep(i, N) {
        if (vp[i].second == vp[i + 1].second) {
            uf.unite(mp[vp[i]], mp[vp[i + 1]]);
        }
    }

    int m = uf.numOfGroups();

    if (m == 1) {
        cout << N - 1 << endl;
        return 0;
    }

    vector<set<int>> st(4);
    rep(i, N) {
        if (vp[i].first == 0)       st[0].insert(uf.root(mp[vp[i]]));
        if (vp[i].first == H - 1)   st[1].insert(uf.root(mp[vp[i]]));
        if (vp[i].second == 0)      st[2].insert(uf.root(mp[vp[i]]));
        if (vp[i].second == W - 1)  st[3].insert(uf.root(mp[vp[i]]));
    }

    int ma = 0;
    rep(i, 4) chmax(ma, (int)st[i].size());

    cout << N + m - 1 - ma << endl;

}

Measured memory_bytes: 6260