Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.

We assume that Bajtek can only heap up snow drifts at integer coordinates.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.

Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.

Output

Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.

Examples

Input

2
2 1
1 2


Output

1


Input

2
2 1
4 1


Output

0
import java.io.*;
import java.util.*;

public class C218 extends C218_Problem {

	void solve() {
		int n = nextInt();
		int[] x = new int[n], y = new int[n];
		for (int i = 0; i < n; i++) {
			x[i] = nextInt();
			y[i] = nextInt();
		}
		Dsu dsu = new Dsu(n);
		for (int i = 0; i < n; i++)
			for (int j = 0; j < n; j++)
				if (i != j && (x[i] == x[j] || y[i] == y[j]))
					dsu.unite(i, j);
		int ans = -1;
		for (int i = 0; i < n; i++)
			if (dsu.find(i) == i)
				ans++;
		print(ans);
	}

	class Dsu {
		int[] p, r;

		Dsu(int n) {
			p = new int[n];
			r = new int[n];
			for (int i = 0; i < n; i++)
				p[i] = i;
		}

		int find(int v) {
			if (p[v] == v)
				return v;
			return p[v] = find(p[v]);
		}

		void unite(int a, int b) {
			a = find(a);
			b = find(b);
			if (a != b) {
				if (r[b] > r[a]) {
					int temp = a;
					a = b;
					b = temp;
				}
				p[b] = a;
				if (r[a] == r[b])
					r[a]++;
			}
		}
	}

	public static void main(String[] args) {
		new Thread(null, new Runnable() {
			public void run() {
				new C218_Solution().run();
			}
		}, "<3", 1 << 23).start();
	}
}

abstract class C218_Problem {

	abstract void solve();

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tokenizer;

	void init(boolean oj) {
		Reader reader = null;
		Writer writer = null;
		if (oj) {
			reader = new InputStreamReader(System.in);
			writer = new OutputStreamWriter(System.out);
		} else
			try {
				reader = new FileReader("input.txt");
				writer = new FileWriter("output.txt");
			} catch (Exception e) {
				e.printStackTrace();
			}
		in = new BufferedReader(reader);
		out = new PrintWriter(writer);
	}

	void exit() {
		out.close();
		System.exit(0);
	}

	void print(Object... obj) {
		for (int i = 0; i < obj.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(obj[i]);
		}
	}

	void println(Object... obj) {
		print(obj);
		print("\n");
	}

	String nextLine() {
		try {
			return in.readLine();
		} catch (Exception e) {
			return "EOF";
		}
	}

	String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}

	int nextInt() {
		return Integer.parseInt(next());
	}

	long nextLong() {
		return Long.parseLong(next());
	}

	double nextDouble() {
		return Double.parseDouble(next());
	}
}

class C218_Solution implements Runnable {
	private boolean oj;
	private long beginTime, beginMem, endTime, endMem;

	public void start() {
		oj = System.getProperty("ONLINE_JUDGE") != null;
		if (!oj) {
			beginTime = System.currentTimeMillis();
			beginMem = totalMemory() - freeMemory();
		}
	}

	public void finish() {
		if (!oj) {
			endMem = totalMemory() - freeMemory();
			endTime = System.currentTimeMillis();
			double mbUsed = 1d * (endMem - beginMem) / 8 / 1024 / 1024;
			double msPass = 1d * (endTime - beginTime) / 1000;
			System.out.println("Memory used = " + mbUsed + " mb");
			System.out.println("Running time = " + msPass + " s");
		}
	}

	public void run() {
		this.start();
		C218_Problem problem = new C218();
		problem.init(oj);
		problem.solve();
		problem.out.close();
		this.finish();
	}

	public long totalMemory() {
		return Runtime.getRuntime().totalMemory();
	}

	public long freeMemory() {
		return Runtime.getRuntime().freeMemory();
	}
}