#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<cstring>
#include<fstream>
#include<cstdio>
#include<utility>
using namespace std;

int n,m,tata[100005],stiva[100005],stlev,t1,t3,xx[100005],yy[100005],x3[100005],y3[100005];
bool sol[100005];
vector<int> intrebari[100005];
int i,j;

const int buffer=1<<13;
char buff[buffer]; int cnt=0;

//getInt() reads and returns one integer at a time
int getInt() {
    int number = 0;
    while(buff[cnt] < '0' || '9' < buff[cnt])
        if(++cnt >= buffer) fread(buff, buffer, 1, stdin), cnt = 0;

    while('0' <= buff[cnt] && buff[cnt] <= '9') {
        number = number * 10 + buff[cnt] - '0';
        if(++cnt >= buffer) fread(buff, buffer, 1, stdin), cnt = 0;
    }

    return number;
}

int radacina(int x) {
   int r=x, aux;
   
   while (tata[r]!=r) r=tata[r];
   
   while (tata[x]!=x) { aux=tata[x]; tata[x]=r; x=aux; }
   
   return r;	
	
}

int main(void) {
    
	//ifstream cin("file.in");	
	
	//n=getInt(); m=getInt();
	cin>>n>>m;
	
	for (i=1; i<=n; ++i) tata[i]=i;
	
	for (i=1; i<=m; ++i) {
		
		int op,x,y;
		cin>>op;
		
		if (op==1) {
			
			//x=getInt();
		//	y=getInt();
		cin>>x>>y;
			
			++t1;
			stiva[++stlev]=t1;
			xx[t1]=x;
			yy[t1]=y;
			
		}
		else if (op==2) {
		
		   // x=getInt();
		   cin>>x;
		    stlev-=x;
		  	
		}
		else {
			
			++t3;
			//x=getInt();
		//	y=getInt();
		cin>>x>>y;
			x3[t3]=x;
			y3[t3]=y;
			
			intrebari[stiva[stlev]].push_back(t3);
			
		}
		
	}
	
	//acum imi construiesc solutia
	
	for (i=1; i<=t1; ++i){
		
		int r1=radacina(xx[i]);
		int r2=radacina(yy[i]);
		
		if (r1!=r2) tata[r1]=r2;
		
		//raspund la intrebari
		for (j=0; j<intrebari[i].size(); ++j){
			
			int index=intrebari[i][j];
			int r1=radacina(x3[index]);
			int r2=radacina(y3[index]);
			
			if (r1==r2) sol[index]=1; else sol[index]=0;
			
		}
		
	}
	
	for (i=1; i<=t3; ++i)
	if (sol[i]==0) cout<<"0\n";
	else cout<<"1\n";
	
	return 0;
}