#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];
vector<pii> sta;

/*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 (u[i]>v[i]) swap(u[i], v[i]);
        if (t[i]==1)
        {
            char s; cin>>s;
            if (s!='a') prvisub=false;
            if (v[i]!=(u[i]+1)) drugisub=false;
            a[i]=(int)(s-'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; }

                int l=u[i], r=v[i]-1;
                bool sigurno=true;
                while (l<=r)
                {
                    bool bul=false;
                    string st="";
                    while (koji[l]==koji[r])
                    {
                        st+=(char)('a'+koji[l]);
                        l++; r--;
                    }
                    int k=0;
                    int pl=l;
                    int smer=1;
                    while (((char)('a'+koji[pl]))==st[k])
                    {
                        if (k<(st.size()-1) && smer) k++;
                        else if (k==(st.size()-1) && smer==1) { smer=0; }
                        else if (smer==0 && k>0) k--;
                        if (smer==0 && k==0) { smer=1; }

                        pl++;
                    }
                    if (k==st.size()-2 && smer==0) bul=true;
                    k=0;
                    int pr=r;
                    smer=1;
                    while (((char)('a'+koji[pr]))==st[k])
                    {
                        if (k<(st.size()-1) && smer) k++;
                        else if (k==(st.size()-1) && smer==1) { smer=0; }
                        else if (smer==0 && k>0) k--;
                        if (smer==0 && k==0) { smer=1; }

                        pr--;
                    }
                    if (k==st.size()-2 && smer==0) bul=true;

                    if (!bul) { sigurno=false; break; }
                }
                if (l<(r-1)) cout<<0<<endl;
                else cout<<1<<endl;
            }
        }
    }
    else assert(0);
}
/*
8
8
1
1
2
a
1
2
3
d
1
3
4
c
1
4
5
d
1
5
6
d
1
6
7
d
1
7
8
a
2
1
8
*/