#include <cstring>
#include <cstdio>
#include <algorithm>
#include <map>
#include <string>
#include <set>
#include <vector>

using namespace std;

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;
    scanf("%d", &number);
    /*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;
}

const int LIM = 350;

int N, M;
int T[LIM + 2][100002], H[LIM + 2][100002];
int STx[100002], STy[100002], ST;
vector<int> V[100002];

int Find(int wh, int x)
{
	if (T[wh][x] != x) T[wh][x] = Find(wh, x);
	return T[wh][x];
}
void Union(int wh, int x, int y)
{
	if (H[wh][x] < H[wh][y])
		T[wh][x] = y;
	else
	{
		T[wh][y] = x;
		if (H[wh][x] == H[wh][y])
			++H[wh][x];
	}
}

int src, fnd;
bool S[100002];

void Dfs(int x)
{
	if (x == src) fnd = 1;
	
	S[x] = true;
	for (auto it = V[x].begin(); it != V[x].end(); ++it)
		if (!S[*it])
			Dfs(*it);
}

int main()
{
	N = getInt();
	M = getInt();

	for (int i = 1; i <= N; ++i)
	{
		T[0][i] = i;
		H[0][i] = 0;
	}
	for (int i = 1; i <= M; ++i)
	{
		int type = getInt();
		
		if (type == 1)
		{
			int x = getInt();
			int y = getInt();
			
			++ST;
			STx[ST] = x;
			STy[ST] = y;
			
			if (ST % LIM == 0)
			{
				int wh = ST / LIM;
				memcpy(T[wh], T[wh - 1], sizeof(T[wh]));
				memcpy(H[wh], H[wh - 1], sizeof(H[wh]));
				
				for (int i = (wh - 1) * LIM + 1; i <= ST; ++i)
					if (Find(wh, STx[i]) != Find(wh, STy[i]))
						Union(wh, STx[i], STy[i]);
			}
		}
		else if (type == 2)
		{
			int tm = getInt();
			ST -= tm;
		}
		else if (type == 3)
		{
			int x = getInt();
			int y = getInt();
			
			int wh = ST / LIM;
			int f1 = Find(wh, x), f2 = Find(wh, y);
			
			for (int i = wh * LIM + 1; i <= ST; ++i)
				if (Find(wh, STx[i]) != Find(wh, STy[i]))
				{
					V[Find(wh, STx[i])].push_back(Find(wh, STy[i]));
					V[Find(wh, STy[i])].push_back(Find(wh, STx[i]));
				}
			
			src = f2, fnd = 0;
			Dfs(f1);
			
			for (int i = wh * LIM + 1; i <= ST; ++i)
			{
				V[Find(wh, STx[i])].clear();
				V[Find(wh, STy[i])].clear();
				S[Find(wh, STx[i])] = false;
				S[Find(wh, STy[i])] = false;
			}
			
			if (fnd == 1) printf("1\n");
			else          printf("0\n");
		}
	}
}