#include <bits/stdc++.h>

using namespace std;
typedef pair<int,char> par;

vector<vector<par>> vv;
vector<bool> posecen;
vector<char> pal;
vector<vector<char>> sp;
stack <int> st;

void dfs(int t, int k){
    posecen[t] = true;
    st.push(t);
    

    while(!st.empty()){
        int cv = st.top(); st.pop();
        for(auto c : vv[cv]){
            if(c.first == k){
                sp.push_back(pal);
                break;
            }
            if(!posecen[c.first]){
                posecen[c.first] = true;
                pal.push_back(c.second);
                st.push(c.first);
            }
            
            
        }
        
    }


}

bool palindrom(){
    bool k = false;
    
    for(int i = 0; i < sp.size(); i++){
        bool t = true;
        int g;
        if (sp[i].size() & 1)
            int g = sp[i].size()/2;
        else
            int g = sp[i].size()/2-1;
        for(int j = 0; j <= g; j++){
            if(sp[i][j] != sp[i][sp[i].size()-j-1])
                t = false;
        }
        if(t)
            k = true; 
    }

        return k;
}


int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n,q; cin >> n >> q;
    vv.resize(n);
    posecen.resize(n,false);

    for(int i = 0; i < q; i++){

        int b,u,v;

        cin >> b >> u >> v;
        u--;v--;

        if(b == 1){
            char c;
            cin >> c;
            vv[u].push_back(par(v,c));
            vv[v].push_back(par(u,c));
        }

        else{
            for(int j = 0; j < posecen.size(); j++){
                posecen[j] = false;
            }
            pal.clear();
            sp.clear();
            dfs(u,v);
            if(palindrom())
                cout << 1 << '\n';
            else
                cout << 0 << '\n';
            
        }
        

    }



    


    return 0;
}