#include<bits/stdc++.h>
#define mp make_pair
#define PII pair<int,int>
#define fi first
#define se second
using namespace std;

const int NMAX=100005;

int n,m,top,f[NMAX],comp[NMAX],viz[NMAX];
PII st[NMAX];

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

void Union(int x,int y)
{
    viz[top]=1;
    if (comp[x]>comp[y])
    {
        f[y]=x;
        comp[x]+=comp[y];
        st[top]=mp(x,y);
    }
    else
    {
        f[x]=y;
        comp[y]+=comp[x];
        st[top]=mp(y,x);
    }
}

int Father(int x)
{
    while (x!=f[x]) x=f[x];
    return x;
}

int main()
{
    int i,j,op,x,y;
   // freopen("date.in","r",stdin);
   // freopen("date.out","w",stdout);
    n=getInt();
    m=getInt();
    for (i=1;i<=n;i++) f[i]=i,comp[i]=1;
    for (i=1;i<=m;i++)
    {
        op=getInt();x=getInt();
        if (op==1)
        {
            y=getInt();
            st[++top]=mp(x,y);viz[top]=0;
            if (Father(x)!=Father(y)) Union(Father(x),Father(y));
        }
        if (op==2)
        {
            for (j=1;j<=x;j++,top--)
                if (viz[top]==1)
                {
                    comp[st[top].fi]-=comp[st[top].se];
                    f[st[top].se]=st[top].se;
                }

        }
        if (op==3)
        {
            y=getInt();
            if (Father(x)==Father(y)) cout<<"1\n";
            else cout<<"0\n";
        }
    }
    return 0;
}