#include <cstdio>
#include <algorithm>
#include <stack>
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;
}

int tata[Nmax], rang[Nmax];
stack < pair < pair <int,int> , 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;

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

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

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


   for ( int i = 1; i <= M; ++i ){
        t = getInt();
       // scanf ( "%d", &t );

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

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

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

            if ( tX != tY )
                tata[y] = tX;

            St.push ( make_pair ( make_pair ( tX, tY ), y ) );

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

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

            for ( int j = 1; j <= x; ++j ){
                pair < pair <int,int> , int > aux = St.top();
                St.pop();

                tata[aux.second] = aux.first.second;
            }

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

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

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

            if ( tX == tY )
                printf ( "1\n" );
            else
                printf ( "0\n" );

            continue;
        }
   }

    return 0;
}