#include <bits/stdc++.h>
#define ll long long
#define MAXN 1010
using namespace std;
ll d[MAXN];
ll Root(ll x)
{
    while (d[x]!=x)
    {
        d[x]=d[d[x]];
        x=d[x];
    }
    return x;
}
void Join(ll x,ll y)
{
    d[Root(x)]=d[Root(y)];
}
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,q,t,u,v;
    char c;
    cin >> n >> q;
    for (ll i=1;i<=n;i++)
        d[i]=i;
    while (q--)
    {
        cin >> t >> u >> v;
        if (t==1)
        {
            cin >> c;
            Join(u,v);
            continue;
        }
        if (Root(u)==Root(v))
            cout << "1\n";
        else
            cout << "0\n";
    }
    return 0;
}