#include <bits/stdc++.h>

using namespace std;

vector<pair<int, char> > g[1005];
bool visited[1005];
void dfs(int x){
    if(visited[x]) return;
    visited[x] = true;
    for(auto u:g[x]) dfs(u.first);
}
int main()
{
    int n,q;
    cin>>n>>q;
    map<<<int, int>,int> urajdeno;
    while(q--){
        int r;
        cin>>r;
        if(r==2){
            int u,v;
            cin>>u>>v;
            dfs(u);
            if(visited[v]) cout<<1<<endl;
            else cout<<0<<endl;
            for(int i=0;i<1005;i++) visited[i] = 0;
        }
        if(r==1){
            int u,v;
            cin>>u>>v;
            if(v!=u+1) return 0;
            char c;
            cin>>c;
            if(uradjeno[{u,v}]!=1) continue;
            g[u].push_back({v,c});
            g[v].push_back({u,c});
        }
    }
}