#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)];
}
char p[MAXN][MAXN];
int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    ll n,q,t,u,v;
    bool yes=true;
    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);
            if (v!=u+1)
                yes=false;
            p[u][v]=c;
            p[v][u]=c;
            continue;
        }
        if (yes)
        {
            if (Root(u)!=Root(v))
            {
                cout << "0\n";
                continue;
            }
            ll cur=max(u,v);
            string s="";
            while (cur!=min(u,v))
            {
                s+=p[cur][cur-1];
                cur--;
            }
            ll l=0,r=s.size()-1;
            bool pal=true;
            while (l<=r)
            {
                if (s[l]!=s[r])
                {
                    pal=false;
                    break;
                }
                l++;
                r--;
            }
            if (pal)
                cout << "1\n";
            else
                cout << "0\n";
            continue;
        }
        if (Root(u)!=Root(v))
            cout << "1\n";
        else
            cout << "0\n";
    }
    return 0;
}