#include <bits/stdc++.h>
#define endl '\n'
#define int long long

using namespace std;

const int MAXN=1000+9,MAXQ=300000+9;
int n,q;
vector<vector<int>> Q(0);
vector<int> par(MAXN,0),rnk(MAXN,0);
int ulp(int node){
    if(par[node]==node) return node;
    return par[node]=ulp(par[node]);
}
void spajaj(int a,int b){
    a=ulp(a);b=ulp(b);
    if(a==b) return;
    if(rnk[b]>rnk[a])
        swap(a,b);
    if(rnk[a]==rnk[b])
        rnk[a]++;
    par[b]=a;
}
void radi1(){
    for(int i=1; i<=n; i++)
        par[i]=i;
    for(int i=0; i<q; i++){
        int t=Q[i][0],a=Q[i][1],b=Q[i][2];
        if(t==1)
            spajaj(a,b);
        else
            cout << (ulp(a)==ulp(b)?1:0) << endl;
    }
}




signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    cin >> n >> q;
    int qq=q;
    bool za1=true;
    while(qq--){
        int t;
        cin >> t;
        if(t==1){
            int a,b;
            char c;
            cin >> a >> b >> c;
            Q.push_back({t,a,b,c-'a'});
            if(c!='a')
                za1=false;
        }
        else{
            int a,b;
            cin >> a >> b;
            Q.push_back({t,a,b});
        }
    }
    if(za1){
        radi1();
        return 0;
    }

    return 0;
}