#include<bits/stdc++.h>
using namespace std;
long long N,Q;
char c;
vector<int>G[1009];
vector<char>Gc[1009];
vector<int>put;
bool posecen[1009];
bool mogu(int u,int v){
  if(u==v){
    for(int i=0; i<put.size()/2; i++)if(put[i]!=put[put.size()-i-1])return false;
    return true;
  }
  for(int i=0; i<G[u].size(); i++){
    int y=G[u][i];
    if(posecen[y])continue;
    put.push_back(Gc[u][i]);
    posecen[y]=true;
    if(mogu(y,v))return true;
    posecen[y]=false;
    put.pop_back();
  }
  return false;
}
int T,u,v;
int main(){
  //freopen("C1.txt","r",stdin);
  ios_base::sync_with_stdio(false);
  cin>>N>>Q;
  while(Q--){
    cin>>T>>u>>v;
    if(T==1){
      cin>>c;
      G[u].push_back(v);
      Gc[u].push_back(c);
      G[v].push_back(u);
      Gc[v].push_back(c);
    }
    if(T==2){
      for(int i=1; i<=N; i++)posecen[i]=false;
      put.clear();
      posecen[u]=true;
      if(mogu(u,v))cout<<"1\n";
      else cout<<"0\n";
      posecen[u]=false;
    }
  }
  return 0;
}