#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5+5;
struct dsu {
int n=0,top=0;
int fa[N],sz[N],stk[N];
void ins(){
n++;
fa[n]=n;
sz[n]=1;
}
void init(int x){
while (x--){
ins();
}
}
int ff(int x){
return fa[x]==x?x:fa[x];
}
void uni(int x,int y){
x=ff(x);
y=ff(y);
if (x==y){
return;
}
if (sz[x]<sz[y]){
swap(x,y);
}
stk[++top]=y;
fa[y]=x;
sz[x]+=sz[y];
}
bool sm(int x,int y){
return ff(x)==ff(y);
}
void del(){
if (!top){
return;
}
int y=stk[top--];
sz[fa[y]]-=sz[y];
fa[y]=y;
}
void dto(int x){
while (top>x){
del();
}
}
} d;
int n,m,t[N],a[N],b[N],op[N],ans[N];
vector<int> g[N];
void dfs(int u){
t[u]=d.top;
if (op[u]==1){
d.uni(a[u],b[u]);
}
if (op[u]==3){
ans[u]=d.sm(a[u],b[u]);
}
for (auto v : g[u]){
dfs(v);
}
d.dto(t[u]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
d.init(n);
for (int i=1; i<=m; i++){
cin>>op[i];
if (op[i]==1 || op[i]==3){
cin>>a[i]>>b[i];
g[i-1].push_back(i);
}
else{
cin>>a[i];
g[a[i]].push_back(i);
}
}
dfs(0);
for (int i=1; i<=m; i++){
if (op[i]==3){
cout<<ans[i]<<endl;
}
}
return 0;
}