#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ---------------------------------------------------------------------------------------------------- */

struct UnionFind {
  int n,num;
  vector<int> r,p;
  UnionFind() {}
  UnionFind(int n):n(n),num(n),r(n,1),p(n,0){iota(p.begin(),p.end(),0);}
  int find(int x) {
    return (x==p[x]?x:p[x]=find(p[x]));
  }
  bool same(int x,int y) {
    return find(x)==find(y);
  }
  void unite(int x,int y) {
    x=find(x);y=find(y);
    if (x==y) return;
    if (r[x]<r[y]) swap(x,y);
    r[x]+=r[y];
    p[y]=x;
    num--;
  }
  int size(int x) {
    return r[find(x)];
  }
  int count() {
    return num;
  }
};

int N,W,H;
int x[40010],y[40010];
vector<int> vx[100010],vy[100010];

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> N >> W >> H;
  int flag = 0;
  REP(i,N) {
    cin >> x[i] >> y[i];
    x[i]--; y[i]--;
    vx[x[i]].push_back(i);
    vy[y[i]].push_back(i);
    if (x[i] == 0 || x[i] == W-1 || y[i] == 0 || y[i] == H-1) flag = 1;
  }
  UnionFind uf(N);
  REP(i,100010) {
    for (int j = 0; j+1 < vx[i].size(); j++) {
      uf.unite(vx[i][j],vx[i][j+1]);
    }
    for (int j = 0; j+1 < vy[i].size(); j++) {
      uf.unite(vy[i][j],vy[i][j+1]);
    }
  }
  set<pii> st;
  REP(i,N) st.insert(pii(uf.find(i),uf.size(i)));
  int ans = 0;
  for (auto p : st) ans += p.second-1;
  if (uf.count() > 1) ans += 2*uf.count()-1-flag;
  cout << ans << endl;

  return 0;
}

### Prompt

Construct a cpp code solution to the problem outlined:

There is a grid of size W × H surrounded by walls. Some cells in the grid are occupied by slimes. The slimes want to unite with each other and become a "King Slime".

In each move, a slime can move to east, west, south and north direction until it enters a cell occupied by another slime or hit the surrounding wall. If two slimes come together, they unite and become a new slime.

Your task is write a program which calculates the minimum number of moves that all the slimes unite and become a King Slime. Suppose slimes move one by one and they never move simultaneously.



Input

The first line contains three integers N (2 ≤ N ≤ 40,000), W and H (1 ≤ W, H ≤ 100,000), which denote the number of slimes, the width and the height of the grid respectively.

The following N lines describe the initial coordinates of the slimes. The i-th line contains two integers xi (1 ≤ xi ≤ W) and yi (1 ≤ yi ≤ H), which indicate the coordinates of the i-th slime . All the coordinates are 1-based.

You may assume that each cell is occupied by at most one slime initially.

Output

Output the minimum number of moves that all the slimes unite and become a King Slime.

Examples

Input

4 3 3
1 1
1 3
3 1
3 3


Output

3


Input

2 3 3
2 2
3 3


Output

2


Input

2 4 4
2 2
3 3


Output

3


Input

2 4 4
2 2
2 3


Output

1

### Response

```cpp
#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define POW(n) (1LL<<(n))
#define IN(i,a,b) (a <= i && i <= b)
using namespace std;
template <typename T> inline bool CHMIN(T& a,T b) { if(a>b) { a=b; return 1; } return 0; }
template <typename T> inline bool CHMAX(T& a,T b) { if(a<b) { a=b; return 1; } return 0; }
template <typename T> inline void SORT(T& a) { sort(ALL(a)); }
template <typename T> inline void REV(T& a) { reverse(ALL(a)); }
template <typename T> inline void UNI(T& a) { sort(ALL(a)); a.erase(unique(ALL(a)),a.end()); }
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ---------------------------------------------------------------------------------------------------- */

struct UnionFind {
  int n,num;
  vector<int> r,p;
  UnionFind() {}
  UnionFind(int n):n(n),num(n),r(n,1),p(n,0){iota(p.begin(),p.end(),0);}
  int find(int x) {
    return (x==p[x]?x:p[x]=find(p[x]));
  }
  bool same(int x,int y) {
    return find(x)==find(y);
  }
  void unite(int x,int y) {
    x=find(x);y=find(y);
    if (x==y) return;
    if (r[x]<r[y]) swap(x,y);
    r[x]+=r[y];
    p[y]=x;
    num--;
  }
  int size(int x) {
    return r[find(x)];
  }
  int count() {
    return num;
  }
};

int N,W,H;
int x[40010],y[40010];
vector<int> vx[100010],vy[100010];

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  cin >> N >> W >> H;
  int flag = 0;
  REP(i,N) {
    cin >> x[i] >> y[i];
    x[i]--; y[i]--;
    vx[x[i]].push_back(i);
    vy[y[i]].push_back(i);
    if (x[i] == 0 || x[i] == W-1 || y[i] == 0 || y[i] == H-1) flag = 1;
  }
  UnionFind uf(N);
  REP(i,100010) {
    for (int j = 0; j+1 < vx[i].size(); j++) {
      uf.unite(vx[i][j],vx[i][j+1]);
    }
    for (int j = 0; j+1 < vy[i].size(); j++) {
      uf.unite(vy[i][j],vy[i][j+1]);
    }
  }
  set<pii> st;
  REP(i,N) st.insert(pii(uf.find(i),uf.size(i)));
  int ans = 0;
  for (auto p : st) ans += p.second-1;
  if (uf.count() > 1) ans += 2*uf.count()-1-flag;
  cout << ans << endl;

  return 0;
}

```