#include <bits/stdc++.h>
#define pc pair<int, string>
#define f first
#define s second
#define ll long long

using namespace std;

bool is(string s)
{
    int n = s.length();
    int i =0, j = n-1;
    while(i<j)
    {
        if(s[i++]!=s[j--])return false;
    }
    return true;
}

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    ll n, q;cin>>n>>q;
    vector< vector<ll> >g(n+10);

    while(q--)
    {
        ll qq;cin>>qq;
        if(qq==1)
        {
            ll a, b;
            string s;
            cin>>a>>b>>s;
            g[a].push_back(b);
            g[b].push_back(a);

        }else
        {
            ll a, b;cin>>a>>b;
            bool bb = false;

            queue<int>q;
            q.push(a);
            vector<bool>visited(n+5, false);
            while(!q.empty())
            {
                int tr = q.front();
                q.pop();

                if(tr==b)
                {
                    bb = true;
                    break;
                }

                if(visited[tr])continue;
                visited[tr]=true;

                for(int i = 0; i<g[tr].size(); i++)
                {
                    q.push(g[tr][i]);
                }
            }
            cout<<bb<<"\n";

        }

        /*for(int i = 1; i<=n; i++)
        {
            for(int j = 1; j<=n; j++)
            {
                cout<<moze[i][j]<<" ";
            }
            cout<<"\n";
        }
        cout<<"--------\n";*/
    }
    return 0;
}