/*#include <cstdio>
#include <stack>*/
#include <iostream>
#include <string>
using namespace std;

#define Nmax 100002

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

string buffer;
string :: iterator buffer_it;

int getInt (){
    while ( *buffer_it<'0' || *buffer_it>'9' ) {
        ++buffer_it;
    }
    int x = 0;
    while ( *buffer_it>='0' && *buffer_it<='9' ) {
        x = x*10+*buffer_it-'0';
        ++buffer_it;
    }
    return x;
}


int tata[Nmax], st[Nmax];
//stack < int > St;

int Find ( int x ){
    int rad, aux;

    for ( rad = x; rad != tata[rad]; rad = tata[rad] );

   /* while ( x != tata[x] ){
        aux = tata[x];
        tata[x] = rad;
        x = aux;
    }*/

    return rad;
}

int main() {

   int N, M, t, x, y, tX, tY;

    getline ( cin, buffer, (char)0 );

    buffer_it = buffer.begin();

   N = getInt();
   M = getInt();

   //scanf ( "%d%d", &N, &M );

   for ( int i = 1; i <= N; ++i )
        tata[i] = i;


   while ( M-- ){
        t = getInt();
        //scanf ( "%d", &t );

        if ( t == 1 ){
            x = getInt();
            y = getInt();


           // scanf ( "%d%d", &x, &y );

            tX = Find(x);
            tY = Find(y);

            //St.push ( tY );
            st[++st[0]] = tY;

            tata[tY] = tX;

        }
        else if ( t == 2 ){
            x = getInt();

            //scanf ( "%d", &x );

            while ( x-- ){
                /*int aux = St.top();
                St.pop();
                tata[aux] = aux;*/
                tata[st[st[0]]] = st[st[0]];
                st[0]--;
            }

        }
        else if ( t == 3 ){
            x = getInt();
            y = getInt();

           // scanf ( "%d%d", &x, &y );

            tX = Find(x);
            tY = Find(y);

            if ( tX == tY )
                cout << 1 << '\n';
                //printf ( "1\n" );
            else
                cout << 0 << '\n';
                //printf ( "0\n" );

        }
   }

    return 0;
}