#include <cstdio>
#include <stdio.h>
using namespace std;

const int buffer = 1 << 15;
char buff[buffer];
int cnt = 0;
int sef[100001];
int operatii[100001], c = 0;
char rez[100001];
int r = 0;

inline int getInt();
inline bool find(int x, int y);
inline void unire(int x, int y);
inline void dezunire(int x);
inline void initial();

int main() {
	initial();
	int n = getInt(), m = getInt();
	int op, x, y;
	for (int i = 0; i < m; i++) {
		op = getInt();
		x = getInt();
		if (op == 2) {
			dezunire(x);
			continue;
		}
		y = getInt();
		if (op == 1)
			unire(x, y);
		if (op == 3) {
			rez[r++] = find(x, y) + '0';
			rez[r++] = '\n';
		}
	}
	fwrite(rez, r, 1, stdout);
}

void initial() {
	for (int i = 0; i < 100001; i++)
		sef[i] = i;
}

void dezunire(int x) {
	while (x--) {
		sef[operatii[c - 1]] = operatii[c - 1];
		c--;
	}
}

void unire(int x, int y) {
	while (x != sef[x])
		x = sef[x];
	while (y != sef[y])
		y = sef[y];
	sef[x] = y;
	operatii[c++] = x;
}

bool find(int x, int y) {
	while (x != sef[x])
		x = sef[x];
	while (y != sef[y])
		y = sef[y];
	if (x == y)
		return true;
	return false;
}


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;
}