#include <bits/stdc++.h>

using namespace std;

//vector<pair<int, char>> g[1005];
/*bool visited[1005];
void dfs(int x){
    if(visited[x]) return;
    visited[x] = true;
    for(auto u:g[x]) dfs(u.first);
}*/
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    #define int long long
    int n,q;
    cin>>n>>q;
    int parent[n+1];
    char krece[n+1] = {};
    for(int i=1;i<=n;i++) krece[i] = ' ';
    for(int i=1;i<=n;i++) parent[i] = i;
    vector<int> moji[n+1];
    bool sveda = true;
    for(int i=1;i<=n;i++) moji[i].push_back(i);
    while(q--){
        int r;
        cin>>r;
        if(r==2){
            int u,v;
            cin>>u>>v;
            if(sveda){
                if(parent[u]==parent[v]) cout<<1<<endl;
                else cout<<0<<endl;
            }
            else{
                if(u>v) swap(u,v);
                string s = "";
                bool celo = true;
                for(int i=u;i<v;i++){
                    if(krece[i]==' '){
                        cout<<0<<endl;
                        celo = false;
                        break;
                    }
                    else s.push_back(krece[i]);
                }
                if(!celo) {
                    cout<<0<<endl;
                    continue;
                }
                string s1 = s;
                reverse(s.begin(), s.end());
                if(s==s1) cout<<1<<endl;
                else cout<<0<<endl;
            }
            continue;
        }
        else if(r==1){
            int u,v;
            cin>>u>>v;
            char c;
            cin>>c;
            if(c!='a') sveda = false;
                if(u<v) krece[u] = c;
                else krece[v] = c;
            //g[u].push_back({v,c});
            //g[v].push_back({u,c});
            if(parent[u]==parent[v]) continue;
            bool da = false;
            if(moji[parent[u]].size()<moji[parent[v]].size()){
            //parent[u] = v;
            int st = parent[u];
            for(auto x:moji[parent[u]]){
                moji[parent[v]].push_back(x);
                parent[x] = parent[v];
            }
            parent[u] = parent[v];
            da  = true;
            //moji[st].clear();
        }
        if(!da){
            int st = parent[v];
            for(auto x:moji[parent[v]]){
                moji[parent[u]].push_back(x);
                parent[x] = parent[u];
            }
            parent[v] = parent[u];
            //moji[st].clear();
        }
        }
    }
}