#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;

bool palindrom(string s){
    int i=0, j=s.size()-1;
    while(i <= j){
        if(i != j)return false;
        i++; j--;
    }
    return true;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    int n,q; cin >> n >> q;
    vector<vector<pair<int, char>>> adj(n);
    while(q--){
        int t; cin >> t;
        int u,v; cin >> u >> v;
        u--; v--;
        if(t==1){
            char c; cin >> c;
            adj[u].emplace_back(v,c);
            adj[v].emplace_back(u,c);
        }
        else {
            queue<pair<int,string>> q;
            map<int, bool> vis;
            q.push({u,""});
            while(!q.empty()){
                string s=q.front().second;
                int tr=q.front().first;
                if(tr == v && palindrom(s)){
                    cout << 1 << '\n';
                    goto dun;
                }
                else{
                q.pop();
                for(auto par : adj[tr]){
                    s.push_back(par.second);
                    if(!vis[par.first]){
                        q.push({par.first,s});
                        vis[par.first]=true;
                    }
                    s.pop_back();
                }
                }
            }
            cout << 0 << '\n';
            dun:;
        }
    }
    return 0;
}