/*************************************************************

http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=DSL_1_A&lang=jp

Sample 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


Sample Output

0
0
1
1
1
0
1
1

*************************************************************/

/************************************************************/
/*	File name	:	main.c									*/
/*	Over view	:	Disjoint Set: Union Find Tree			*/
/*	======================================================	*/
/*	Auther					:	H.Futaeda					*/
/*	Date of first creation	:	2018/07/06					*/
/************************************************************/

/*	Include header file										*/
#include <stdio.h>
#include <stdlib.h>

/*	Define structur for set's elements						*/
typedef struct Element_{
	int				num;
	struct Element_	*next_element;
}Element;

/*	Define structur for sets								*/
typedef struct Set_{
	Element		*first_element;
	struct Set_	*next_set;
}Set;

/*	Define structur for queries								*/ 
typedef struct Querie_{
	int				com;
	int				x;
	int				y;
	struct Querie_	*next_querie;
}Querie;

/*	Function prototype										*/
Set		*make_set(void);
Element	*make_element(int num);
Querie	*make_querie(int com, int x, int y);
void	unite_set(Set *first_set, int x, int y);
int		match_element(Set *first_set, int x, int y);

/************************************************************/
/*	Function name											*/
/*		main												*/
/*	Over view												*/
/*		Main function.										*/
/*	------------------------------------------------------	*/
/*	How to call												*/
/*		int	main(void);										*/
/*	Parameter												*/
/*		None												*/
/*	Return													*/
/*		0	:	Success complete							*/
/************************************************************/
int	main(void)
{
	int		n, q;
	int		com, x, y;
	int		ii;
	Set		*first_set = NULL;
	Querie	*first_querie = NULL;
	Set		*set = NULL;
	Querie	*querie = NULL;
	Element *element = NULL;
	
	scanf("%d %d", &n, &q);
	
	first_set = make_set();
	set = first_set;
	
	ii = 1;
	while(ii < n){
		set->next_set = make_set();
		set = set->next_set;
		ii++;
	}

	scanf("%d %d %d", &com, &x, &y);
	first_querie = make_querie(com, x, y);
	querie = first_querie;

	ii = 1;
	while(ii < q){
		scanf("%d %d %d", &com, &x, &y);
		querie->next_querie = make_querie(com, x, y);
		querie = querie->next_querie;
		ii++;
	}
    
	querie = first_querie;
	
	ii = 0;
	do{
		switch(querie->com){
		case 0:
			unite_set(first_set, querie->x, querie->y);
			break;
		case 1:
			printf("%d\n", match_element(first_set, querie->x, querie->y));
			break;
		default:
			break;
		}
		querie = querie->next_querie;
	}while(querie != NULL);
	
	return 0;
}

/************************************************************/
/*	Function name											*/
/*		make_set											*/
/*	Over view												*/
/*		Make new set.										*/
/*	------------------------------------------------------	*/
/*	How to call												*/
/*		Set *make_set(void);								*/
/*	Parameter												*/
/*		None												*/
/*	Return													*/
/*		Set	*set	:	Pointer of new set					*/
/************************************************************/
Set *make_set(void)
{
    static int  set_cnt = 0;
    Set         *set = NULL;
    
    set = (Set *)malloc(sizeof(Set));
    
    set->first_element = make_element(set_cnt);
    set->next_set = NULL;
    
    set_cnt++;
    
    return set;
}

/************************************************************/
/*	Function name											*/
/*		make_element										*/
/*	Over view												*/
/*		Make new element.									*/
/*	------------------------------------------------------	*/
/*	How to call												*/
/*		Element *make_element(int num);						*/
/*	Parameter												*/
/*		int		num			:	element number				*/
/*	Return													*/
/*		Element	*element	:	Pointer of new element		*/
/************************************************************/
Element *make_element(int num)
{
	Element *element = NULL;

	element = (Element *)malloc(sizeof(Element));

	element->num = num;
	element->next_element = NULL;

	return element;
}

/************************************************************/
/*	Function name											*/
/*		make_querie											*/
/*	Over view												*/
/*		Make new querie.									*/
/*	------------------------------------------------------	*/
/*	How to call												*/
/*		Querie *make_querie(int com, int x, int y);			*/
/*	Parameter												*/
/*		int		com		:	com number						*/
/*		int		x		:	element number of x				*/
/*		int		y		:	element number of y				*/
/*	Return													*/
/*		Querie	*querie	:	Pointer of new querie			*/
/************************************************************/
Querie *make_querie(int com, int x, int y)
{
	Querie *querie = NULL;

	querie = (Querie *)malloc(sizeof(Querie));

	querie->com = com;
	querie->x = x;
	querie->y = y;
	querie->next_querie = NULL;

	return querie;
}

/************************************************************/
/*	Function name											*/
/*		unite_set											*/
/*	Over view												*/
/*		Unite set of inclusive element (x and y).			*/
/*	------------------------------------------------------	*/
/*	How to call												*/
/*		void unite_set(Set *first_set, int x, int y);		*/
/*	Parameter												*/
/*		Set		*first_set	:	pointer of first set		*/
/*		int		x			:	element number of x			*/
/*		int		y			:	element number of y			*/
/*	Return													*/
/*		None												*/
/************************************************************/
void unite_set(Set *first_set, int x, int y)
{
	Set     *set = first_set;
	Set     *prev_set = NULL;
	Set     *setx = NULL;
	Set     *sety = NULL;
	Set     *prev_setx = NULL;
	Set     *prev_sety = NULL;
	Element *element = first_set->first_element;
	
	/*	Find set of inclusive element x y						*/
	/*	If setx and sety is NULL, continue loop					*/
	while(setx == NULL || sety == NULL){
		
		/*	If element number equal x, save the set x				*/
	    if(element->num == x){
			setx = set;
			prev_setx = prev_set;
	    	
		/*	If element number equal y, save the set y				*/
	    }else if(element->num == y){
			sety = set;
			prev_sety = prev_set;
            
        }
        
        /*	If next element is NULL, change set to next				*/        
        if(element->next_element == NULL){
            /*	If next set is NULL, end the loop						*/
			if(set->next_set == NULL){
				break;
			}
			prev_set = set;
            /*	change set to next										*/
			set = set->next_set;
            /*	change element to next that next set's first element	*/
			element = set->first_element;
            
	    }else{
            /*	change element to next									*/
            element = element->next_element;
            	
	    }
	}
    
    /*  unite set                                                   */
	if(x < y){
        element = setx->first_element;
        while(element->next_element != NULL){
            element = element->next_element;
        }
        element->next_element = sety->first_element;
		if(prev_sety != NULL){
            prev_sety->next_set = sety->next_set;
		}
        free(sety);
	}else{
        element = sety->first_element;
        while(element->next_element != NULL){
            element = element->next_element;
        }
        element->next_element = setx->first_element;
		if(prev_setx != NULL){
			prev_setx->next_set = setx->next_set;
		}
        free(setx);
	}
}

/************************************************************/
/*	Function name											*/
/*		match_element										*/
/*	Over view												*/
/*		Find set of inclusive element (x and y) .			*/
/*	------------------------------------------------------	*/
/*	How to call												*/
/*		int match_element(Set *first_set, int x, int y);	*/
/*	Parameter												*/
/*		Set		*first_set	:	pointer of first set		*/
/*		int		x			:	element number of x			*/
/*		int		y			:	element number of y			*/
/*	Return													*/
/*		int		found_flg	:								*/
/*			1 is same that found sets						*/
/*			0 is not same that found sets					*/
/************************************************************/
int match_element(Set *first_set, int x, int y)
{
    Set     *set = first_set;
    Element *element = first_set->first_element;
    int     found_flg = 0;
    
    while(set != NULL){
        if(element->num == x){
            found_flg++;
            if(found_flg == 2){
                break;
            }
        }else if(element->num == y){
            found_flg++;
            if(found_flg == 2){
                break;
            }
        }
        
        if(element->next_element == NULL){
            if(found_flg == 1){
                break;
            }else if(set->next_set == NULL){
                break;
            }else{
                set     = set->next_set;
                element = set->first_element;
            }
        }else{
            element = element->next_element;
        }
    }
    
    return found_flg / 2;
}
