#include <bits/stdc++.h>

using namespace std;

int n,q,T[300010],L[300010],R[300010],B[300010];
bool p;
char C[300010];
vector<int> G[300010];
vector<pair<int,char>> V[300010];
vector<char> S;

void spoj(int x,int y)
{
    if(G[x].size()>G[y].size()) swap(x,y);
    for(int i=0; i<G[x].size(); i++)
    {
        G[y].push_back(G[x][i]);
        B[G[x][i]] = y;
    }
    G[x].clear();
    return;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> q;
    for(int i=1; i<=q; i++)
    {
        cin >> T[i] >> L[i] >> R[i];
        if(T[i]==1)
        {
            cin >> C[i];
            if(C[i]!='a') p = 1;
        }
    }
    if(!p)
    {
        for(int i=1; i<=n; i++)
        {
            G[i] = vector<int>{i};
            B[i] = i;
        }
        for(int i=1; i<=q; i++)
        {
            if(T[i]==2)
            {
                if(B[L[i]]==B[R[i]]) cout << 1 <<endl;
                else cout << 0 <<endl;
                continue;
            }
            if(B[L[i]]!=B[R[i]]) spoj(B[L[i]],B[R[i]]);
        }
        return 0;
    }
    for(int i=1; i<=q; i++)
    {
        if(T[i]==1)
        {
            V[L[i]].push_back({R[i],C[i]});
            V[R[i]].push_back({L[i],C[i]});
            continue;
        }
        S.clear();
        if(L[i]>R[i]) swap(L[i],R[i]);
        int b = L[i];
        while(b!=R[i])
        {
            if(V[b].size()>0)
            {
                if(V[b][0].first==b+1)
                {
                    S.push_back(V[b][0].second);
                    b++;
                }
                else
                {
                    if(V[b].size()>1)
                    {
                        if(V[b][1].first==b+1)
                        {
                            S.push_back(V[b][1].second);
                            b++;
                        }
                        else break;
                    }
                    else break;
                }
            }
            else break;
        }
        if(b!=R[i])
        {
            cout << 0 <<endl;
            continue;
        }
        p = 0;
        for(int j=0; j<S.size()-j-1; j++)
        {
            if(S[j]!=S[S.size()-j-1]) p = 1;
        }
        if(p) cout << 0 <<endl;
        else cout << 1 <<endl;
    }
    return 0;
}