Problem: Write a program which manipulates a disjoint set S = {S1, S2, . . . , Sk}.

First of all, the program should read an integer n, then make a disjoint set where each element consists of 0, 1, ... n−1 respectively.

Next, the program should read an integer q and manipulate the set for q queries. There are two kinds of queries for different operations:

* unite(x, y): unites sets that contain x and y, say Sx and Sy, into a new set.
* same(x, y): determine whether x and y are in the same set.

Constraints

* 1 ≤ n ≤ 10000
* 1 ≤ q ≤ 100000
* x ≠ y

Input


n q
com1 x1 y1
com2 x2 y2
...
comq xq yq


In the first line, n and q are given. Then, q queries are given where com represents the type of queries. '0' denotes unite and '1' denotes same operation.

Output

For each same operation, print 1 if x and y are in the same set, otherwise 0, in a line.

Example

Input

5 12
0 1 4
0 2 3
1 1 2
1 3 4
1 1 4
1 3 2
0 1 3
1 2 4
1 3 0
0 0 4
1 0 2
1 3 0


Output

0
0
1
1
1
0
1
1

Solution (in JAVA):

Solution:
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
//		System.out.println("Started");
		Main main = new Main();
		main.process();
	}
	
	public void process() {
		int nodeNum = 0, queryNum = 0;
		Node[] nodes;
		Scanner scanner = new Scanner(System.in);
		nodeNum = scanner.nextInt();
		queryNum = scanner.nextInt();
		nodes = makeSet(nodeNum);
		for(int i = 0; i < queryNum; i++) {
//			System.out.println("i = " + i);
			int command = scanner.nextInt();
//			System.out.print("com: " + command);
			Node target1 = nodes[scanner.nextInt()];
			Node target2 = nodes[scanner.nextInt()];
//			System.out.println(", t1: " + target1.getNum() + ", t2: " + target2.getNum());
			if(command == 0) union(target1, target2);
			else if(command == 1) same(target1, target2);
			else System.err.println("Input command is wrong!!");
		}
	}
	
	public void same(Node x, Node y) {
//		System.out.println("Process " + x.getNum() + " and " + y.getNum() + " same?");
		if(findSet(x).equals(findSet(y))) System.out.println("1");
		else System.out.println("0");
	}
	
	public Node[] makeSet(int x) {
		Node[] nodes = new Node[x];
		for(int i = 0; i < x; i++) nodes[i] = new Node(i);
		return nodes;
	}
	
	public void union(Node x, Node y) {
//		System.out.println("Process " + x.getNum() + " and " + y.getNum() + " union!");
		link(findSet(x), findSet(y));
	}
	
	public void link(Node x, Node y) {
		if(x.getRank() > y.getRank()) y.point(x);
		else {
			x.point(y);
			if(x.getRank() == y.getRank()) y.setRank(y.getRank() + 1);
		}
	}
	
	public Node findSet(Node x) {
		if(x != x.point()) x.point(findSet(x.point()));
		return x.point();
	}

	private class Node{
		private int rank = 0;
		private int num = 0;
		private Node destination;
		public Node(int num) {
			this.num = num;
			this.destination = this;
		}
		public void setRank(int rank) {
			this.rank = rank;
		}
		public int getNum() {
			return this.num;
		}
		public int getRank() {
			return this.rank;
		}
		public void point(Node node) {
			this.destination = node;
		}
		public Node point() {
			return this.destination;
		}
	}
}