#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 a,int b){
  if(a==b)return true;
  posecen[a]=true;
  for(int i=0; i<G[a].size(); i++){
    int c=G[a][i];
    if(posecen[c])continue;
    if(mogu(c,b))return true;
  }
  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;
      if(mogu(u,v))cout<<"1\n";
      else cout<<"0\n";
    }
  }
  return 0;
}