#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];

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);
    cin.sync_with_stdio(false);
    cin>>n>>m;
    for (i=1;i<=n;i++) f[i]=i,comp[i]=1;
    for (i=1;i<=m;i++)
    {
        cin>>op>>x;
        if (op==1)
        {
            cin>>y;
            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)
        {
            cin>>y;
            if (Father(x)==Father(y)) cout<<"1\n";
            else cout<<"0\n";
        }
    }
    return 0;
}