#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define endl '\n'
#define pb push_back
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 1009;
vector<vector<int>> g(maxN);
vector<int> boja(maxN, -1);
int main(){
    ios;
    int n, q;
    cin>>n>>q;
    for(int i = 1;i<=n;i++)boja[i]=i;
    vector<pair<bool,char>> imavezaod(n+1);
    vector<pair<bool,char>> imavezado(n+1);
    bool podzadatak1 = true;
    while(q--){
        int tip;
        cin>>tip;
        if(tip==1){
            int u, v;
            char c;
            cin>>u>>v>>c;
            if(c!='a')podzadatak1 = false;
            imavezaod[u].first=true;
            imavezaod[u].second=c;
            imavezado[v].first=true;
            imavezado[v].second=c;
            int b1 = boja[u];
            int b2 = boja[v];
            for(int i = 1;i<=n;i++){
                if(boja[i]==b2)boja[i]=b1;
            }
        }
        else{
            int u, v;
            cin>>u>>v;
            if(podzadatak1){
                if(boja[u]==boja[v])cout<<1<<endl;
                else cout<<0<<endl;
            }
            else{
                u = min(u, v);
                v = max(u, v);
                int i = u;
                int j = v;
                bool moze = true;
                while(i<=j){
                    if(!imavezaod[i].first||!imavezado[j].first){
                        moze = false;
                        break;
                    }
                    if(imavezaod[i].second!=imavezado[j].second){
                        moze = false;
                        break;
                    }
                    i++;
                    j--;
                }
                if(moze)cout<<1<<endl;
                else cout<<0<<endl;
            }
        }
    }


}