#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-2;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

struct uf {
private:
	vector<int> par, ran;
public:
	void init(int n) {
		par.resize(n, 0);
		ran.resize(n, 0);
		rep(i, n) {
			par[i] = i;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return;
		if (ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (ran[x] == ran[y])ran[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

int n, h, w;
const int mn = 100000;
vector<P> cx[mn], cy[mn];
uf u;
int x[40000], y[40000];
bool used[40000];
int main() {
	cin >> n >> h >> w; u.init(n);
	bool exi = false;
	rep(i, n) {
		cin >> x[i] >> y[i]; x[i]--; y[i]--;
		if (x[i] == 0 || y[i] == 0 || x[i] == h - 1 || y[i] == w - 1)exi = true;
		cx[x[i]].push_back({ y[i],i });
		cy[y[i]].push_back({ x[i],i });
	}
	rep(i, h)sort(cx[i].begin(), cx[i].end());
	rep(i, w)sort(cy[i].begin(), cy[i].end());
	rep(i, n) {
		int id = upper_bound(cx[x[i]].begin(), cx[x[i]].end(), P{ y[i],i }) - cx[x[i]].begin();
		if (id < cx[x[i]].size()) {
			int to = cx[x[i]][id].second;
			u.unite(i, to);
		}
		id -= 2;
		if (id >=0) {
			int to = cx[x[i]][id].second;
			u.unite(i, to);
		}
		id = upper_bound(cy[y[i]].begin(), cy[y[i]].end(), P{ x[i],i }) - cy[y[i]].begin();
		if (id < cy[y[i]].size()) {
			int to = cy[y[i]][id].second;
			u.unite(i, to);
		}
		id -= 2;
		if (id >= 0) {
			int to = cy[y[i]][id].second;
			u.unite(i, to);
		}
	}
	int cnt = 0;
	rep(i, n) {
		int p = u.find(i);
		if (used[p])continue;
		used[p] = true; cnt++;
	}
	int ans = n - cnt;
	if (cnt > 1) {
		ans += 2 * cnt - 1;
		if (exi)ans--;
	}
	cout << ans << endl;
	//stop
	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<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-2;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;

struct uf {
private:
	vector<int> par, ran;
public:
	void init(int n) {
		par.resize(n, 0);
		ran.resize(n, 0);
		rep(i, n) {
			par[i] = i;
		}
	}
	int find(int x) {
		if (par[x] == x)return x;
		else return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y)return;
		if (ran[x] < ran[y]) {
			par[x] = y;
		}
		else {
			par[y] = x;
			if (ran[x] == ran[y])ran[x]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};

int n, h, w;
const int mn = 100000;
vector<P> cx[mn], cy[mn];
uf u;
int x[40000], y[40000];
bool used[40000];
int main() {
	cin >> n >> h >> w; u.init(n);
	bool exi = false;
	rep(i, n) {
		cin >> x[i] >> y[i]; x[i]--; y[i]--;
		if (x[i] == 0 || y[i] == 0 || x[i] == h - 1 || y[i] == w - 1)exi = true;
		cx[x[i]].push_back({ y[i],i });
		cy[y[i]].push_back({ x[i],i });
	}
	rep(i, h)sort(cx[i].begin(), cx[i].end());
	rep(i, w)sort(cy[i].begin(), cy[i].end());
	rep(i, n) {
		int id = upper_bound(cx[x[i]].begin(), cx[x[i]].end(), P{ y[i],i }) - cx[x[i]].begin();
		if (id < cx[x[i]].size()) {
			int to = cx[x[i]][id].second;
			u.unite(i, to);
		}
		id -= 2;
		if (id >=0) {
			int to = cx[x[i]][id].second;
			u.unite(i, to);
		}
		id = upper_bound(cy[y[i]].begin(), cy[y[i]].end(), P{ x[i],i }) - cy[y[i]].begin();
		if (id < cy[y[i]].size()) {
			int to = cy[y[i]][id].second;
			u.unite(i, to);
		}
		id -= 2;
		if (id >= 0) {
			int to = cy[y[i]][id].second;
			u.unite(i, to);
		}
	}
	int cnt = 0;
	rep(i, n) {
		int p = u.find(i);
		if (used[p])continue;
		used[p] = true; cnt++;
	}
	int ans = n - cnt;
	if (cnt > 1) {
		ans += 2 * cnt - 1;
		if (exi)ans--;
	}
	cout << ans << endl;
	//stop
	return 0;
}


```