#include <stdio.h>
#include <string.h>

#define N 10001

struct DSL1A{
	
	char values[N];
	int len;
	
};
typedef struct DSL1A DSL1A;

DSL1A array[N];
int n;

DSL1A newData(int num);

int isFound(DSL1A data, int n);
int getIdx(int num);

void uniteData(int idx1, int idx2);

int main(){
	
	int q;
	int i, k;
	int com, x, y;
	int output;
	int idx1, idx2;
	char temp[100];
	
	fgets(temp, sizeof(temp), stdin);
	sscanf(temp, "%d %d", &n, &q);
	
	for(i=0; i<n; i++){
		
		array[i] = newData(i);
		
		//printf("%s, %d\n", array[i].values, array[i].len);
		
	}
	
	
	for(i=0; i<q; i++){
		
		fgets(temp, sizeof(temp), stdin);
		sscanf(temp, "%d %d %d", &com, &x, &y);
		
		//do unite
		if(com == 0){
			
			idx1 = getIdx(x);
			idx2 = getIdx(y);
			
			//printf("uniteData(%d, %d)\n", idx1, idx2);
			
			uniteData(idx1, idx2);
			
			//printf("united::%s, %d\n", array[idx1].values, array[idx1].len);
			
		}
		
		//do same
		else{
			
			output = 0;
			
			for(k=0; k<n; k++){
				
				if(isFound(array[k], x)){
					
					if(isFound(array[k], y)){
						
						output = 1;
						
					}
					
				}
				
			}
			
			printf("%d\n", output);
			
		}
		
	}
	
	return 0;
	
}



DSL1A newData(int num){
	
	DSL1A ret;
	
	snprintf(ret.values, sizeof(ret.values), "%d", num);
	
	ret.len = strlen(ret.values);
	
	return ret;
	
}



int isFound(DSL1A data, int n){
	
	int i;
	int ret = 0;
	
	for(i=0; i<data.len; i++){
		
		if(data.values[i] == n+'0'){
			
			ret = 1;
			break;
			
		}
		
	}
	
	return ret;
	
}



int getIdx(int num){
	
	int i;
	
	for(i=0; i<n; i++){
		
		if(isFound(array[i], num)){
			
			return i;
			
		}
		
	}
	
	
}


void uniteData(int idx1, int idx2){
	
	
	if(idx1 == idx2){
		
		return;
		
	}
	
	strcat(array[idx1].values, array[idx2].values);
	array[idx1].len = strlen(array[idx1].values);
	
	array[idx2].values[0] = '\0';
	array[idx2].len = strlen(array[idx2].values);
	
}