#include<bits/stdc++.h>
#define ll long long
#define pll pair<ll,ll>
#define pii pair<int,int>
#define all(data) data.begin(),data.end()
#define pb push_back
#define mp make_pair
#define INF 1e15
#define MOD 1000000007
using namespace std;
#define MAXN 200007
#define MAXN 1007
#define MAXQ 300007
ll p[MAXN],sz[MAXN];
void Init(ll n)
{
    for(int i=1;i<=n;i++) {p[i]=i; sz[i]=1;}
}
ll Root(ll u)
{
    if(p[u]==u) return u;
    else return Root(p[u]);
}
void Join(ll u,ll v)
{
    u=Root(u); v=Root(v);
    if(u==v) return;
    if(sz[u]<sz[v]) swap(u,v);
    sz[u]+=sz[v]; p[v]=p[u];
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0);
    ll n,q; cin>>n>>q;
    ll t,u,v,c; Init(n);
    while(q--)
    {
        cin>>t;
        if(t==1)
        {
            cin>>u>>v>>c;
            Join(u,v);
        }
        else
        {
            cin>>u>>v;
            if(Root(u)==Root(v)) cout<<1<<endl;
            else cout<<0<<endl;
        }
    }
    return 0;
}