#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1010;

//A TI SI BADBOY HOTSAUSE USNAMA ME LANSIRAS U KOSMOS

/*
1 1 2 a
1 2 3 a
1 4 5 a
2 1 5
*/
vector<int> graf[N];
bool mark[N];

void dfs(int v){
    mark[v]=true;
    for(auto x : graf[v]){
        if(mark[x])continue;
        dfs(x);
    }
}


signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    while(q--){
        for(int i=0; i <= n; i++)mark[i]=false;
        int tr;
        cin >> tr;
        if(tr==1){
            int x, y;
            char a;
            cin >> x >> y >> a;
            if(a!='a')return 0;
            graf[x].push_back(y);
            graf[y].push_back(x);
        }
        else{
            int x,y;
            cin >> x >> y;
            dfs(x);
            if(mark[y])cout<<1<<"\n";
            else cout <<0<<"\n";
        }

    }


}