#include <bits/stdc++.h>

using namespace std;

vector<pair<int, char>> g[1005];
bool visited[1005];
string rez;
void dfs(int x, string s, int kraj){
    if(visited[x]) return;
    rez = s;
    visited[x] = true;
    if(x == kraj) return;
    for(auto u:g[x]) dfs(u.first, s+u.second, kraj);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n,q;
    cin>>n>>q;
    while(q--){
        int r;
        cin>>r;
        if(r==2){
            int u,v;
            cin>>u>>v;
            rez = "";
            dfs(u, "",v);
            if(visited[v]){
                string rez1 = rez;
                //cerr<<rez<<endl;
                reverse(rez.begin(), rez.end());
                if(rez == rez1) cout<<1<<endl;
                else cout<<0<<endl;
            }
            else {
                cout<<0<<endl;
            }
            for(int i=0;i<1005;i++) visited[i] = 0;
        }
        if(r==1){
            int u,v;
            cin>>u>>v;
            char c;
            cin>>c;
            if(v-1!=u) return 0;
            g[u].push_back({v,c});
            g[v].push_back({u,c});
        }
    }
}