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

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

struct upit
{
    int t, a, b;
    string s;
};

int main()
{

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

    int n, q;cin>>n>>q;
    int q1 = q;

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

    bool prvi = true, drugi = true;

    while(q--)
    {
        int qq;cin>>qq;
        upit uu;
        if(qq==1)
        {
            uu.t = 1;
            int a, b;
            string s;
            cin>>a>>b>>s;
            uu.a=a;
            uu.b=b;
            uu.s=s;
            if(s!="a")prvi=false;
            if(b!=a+1)drugi = false;
        }else
        {
            int a, b;cin>>a>>b;
            uu.t = 2;
            uu.a=a;
            uu.b=b;
        }
        upiti.push_back(uu);
    }
    if(prvi)
    {
        bool moze[n+5][n+5] = {{0}};
        string ss = "";
        for(int i = 0; i<q1; i++)
        {
            upit uu = upiti[i];
            if(upiti[i].t==1)
            {
                moze[uu.a][uu.b]=1;
                for(int j = 1; j<=n; j++)
                {
                    if(moze[j][uu.a])moze[j][uu.b]=1;
                }

            }else
            {
                cout<<moze[uu.a][uu.b]<<"\n";
            }
        }
    }

    return 0;
}