#include using namespace std; const int buffer=1<<13; const int NMAX =100005; 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 Father[NMAX], st[NMAX], k; inline int Find(const int x) { if(Father[x]!=x) Father[x] = Find(Father[x]); return Father[x]; } int main() { /*#ifndef ONLINE_JUDGE freopen("date.in","r",stdin); freopen("date.out","w",stdout); #endif // ONLINE_JUDGE*/ int n = getInt(); int m = getInt(); int operation, x, y; for(int i=1;i<=n;++i) Father[i] = i; while(m--) { operation = getInt(); if(operation == 1) { x = Find(getInt()); y = Find(getInt()); if(x != y) Father[x] = y; st[++k] = x; } else if(operation == 2) { x = getInt(); for(int i=k-x+1;i<=k;++i) Father[st[i]] = st[i]; } else { x = Find(getInt()); y = Find(getInt()); printf("%d\n",(x==y)); } } return 0; }