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

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);

    int n, q;cin>>n>>q;

    vector< vector< pc > >g(n+5);

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

            vector<string> path;
            bool bb = false;
            queue< int > pq;
            pq.push(a);
            vector<bool>visited(n+5, false);
            while(!pq.empty())
            {
                int tr = pq.top();
                pq.pop();
                if(tr==b)
                {
                    cout<<"1\n";
                    bb = true;
                    break;
                }
                if(visited[tr])continue;
                visited[tr]=true;

                for(int u=0; u<g[tr].size(); u++)
                {
                    pq.push(g[tr][u]);
                    if(tr==g[tr][u])
                    {
                        cout<<"1\n";
                        bb = true;
                        break;
                    }
                }
            }
            if(!bb)cout<<"0\n";
        }
    }
    return 0;
}