#include <bits/stdc++.h>

#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define pb push_back
#define MAXN 1010
#define MAXQ 300010
#define INF 1000000000000010LL

using namespace std;

int n, t[MAXQ], u[MAXQ], v[MAXQ], a[MAXQ];
int dsu[MAXN], sz[MAXN];
int koji[MAXN];

/*void dfs(int u)
{
    bio[u]=true;
    for (auto v:g[u])
    {
        if (!bio[v]) dfs(v);
    }
}*/

int root(int u)
{
    while (dsu[u]!=u)
    {
        dsu[u]=dsu[dsu[u]];
        u=dsu[u];
    }
    return u;
}

void connect(int u, int v)
{
    if (root(u)==root(v)) return;

    u=root(u); v=root(v);
    if (sz[u]<=sz[v]) swap(u, v);

    dsu[v]=u;
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int q;
    cin>>n>>q;

    for (int i=1; i<=n; i++) { sz[i]=1; dsu[i]=i; }

    bool prvisub=true, drugisub=true;
    for (int i=0; i<q; i++)
    {
        cin>>t[i]>>u[i]>>v[i];
        if (t[i]==1)
        {
            string s; cin>>s;
            if (s!="a") prvisub=false;
            if (v[i]!=(u[i]+1)) drugisub=false;
            a[i]=(int)(s[0]-' a');
        }
    }

    if (prvisub)
    {
        for (int i=0; i<q; i++)
        {
            if (t[i]==1)
            {
                connect(u[i], v[i]);
            }
            else
            {
                cout<<(root(u[i])==root(v[i]))<<endl;
            }
        }
    }
    else if (drugisub)
    {
        for (int i=0; i<q; i++)
        {
            if (t[i]==1)
            {
                connect(u[i], v[i]);
                koji[u[i]]=a[i];
            }
            else
            {
                if (u[i]>v[i]) swap(u[i], v[i]);
                if ((root(u[i])!=root(v[i]))) { cout<<0<<endl; continue; }
                bool moze=true;
                for (int j=u[i]; j<v[i]; j++)
                {
                    //cout<<j<<" "<<v[i]-u[i]+j-1<<endl;
                    if (koji[j]!=koji[v[i]-j+u[i]-1]) { moze=false; break; }
                }
                cout<<(int)(moze==true)<<endl;
            }
        }
    }
    else assert(0);
}
/*
5
7
1
1
2
a
1
2
3
a
2
1
2
1
4
5
a
2
1
5
1
3
4
a
2
1
5
*/
/*
5
6
1
1
2
a
1
2
3
b
2
1
3
1
3
4
a
2
1
4
2
1
3
*/