#include<bits/stdc++.h>
using namespace std;
long long N,Q,T,u,v;
char c;
vector<int>G[1009];
vector<char>Gc[1009];
bool uspeh;
int otac[1009],dubina[1009];
int otacf(int a){
  while(otac[a]!=a){
    otac[a]=otac[otac[a]];
    a=otac[a];
  }
  return a;
}
void join(int a,int b){
  int x=otacf(a);
  int y=otacf(b);
  if(dubina[x]>dubina[y])otac[y]=x;
  if(dubina[x]<dubina[y])otac[x]=y;
  if(dubina[x]==dubina[y]){
    otac[x]=y;
    dubina[y]++;
  }
}
int main(){
  //freopen("C1.txt","r",stdin);
  ios_base::sync_with_stdio(false);
  cin>>N>>Q;
  for(int i=1; i<=N; i++)otac[i]=i;
  for(int i=1; i<=N; i++)dubina[i]=0;
  while(Q--){
    cin>>T>>u>>v;
    if(T==1){
      cin>>c;
      if(c!='a')exit(2);
      join(u,v);
    }
    if(T==2){
      if(otacf(u)==otacf(v))cout<<"1\n";
      else cout<<"0\n";
    }
  }
  return 0;
}