#include <cstdio>
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() {
    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());
            Father[x] = y;
            st[++k] = x;
        }
        else
            if(operation == 2)
            {
                x = getInt();
                while(x--)
                {
                    Father[st[k]] = st[k];
                    --k;
                }
            }
            else
            {
                x = Find(getInt());
                y = Find(getInt());
                printf("%d\n",(x==y));
            }
    }

    return 0;
}