#include <bits/stdc++.h>
#define int long long

using namespace std;

int mark[1010];
vector<pair<int,char>>graf[1010];

bool dfs(int u,int v,string s,int c){
    if(mark[u]==c)return false;
    mark[u]=c;
    if(u==v){
        int l=0,r=s.size()-1;
        while(l<r){
            if(s[l++]!=s[r--])return false;
        }
        return true;
    }
    for(auto i:graf[u]){
        int nod=i.first;
        if(dfs(nod,v,s+i.second,c))return true;
    }
    return false;
}

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,m,col=1;
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int c;
        cin>>c;
        if(c==1){
            int u,v;
            char ch;
            cin>>u>>v>>ch;
            graf[u].push_back({v,ch});
            graf[v].push_back({u,ch});
        }
        else{
            int u,v;
            cin>>u>>v;
            cout<<dfs(u,v,"",col++)<<endl;
        }
    }
    return 0;
}