#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int MAX=1e6+3;
int n, q, start, target;
bool naso=0;
bool v[MAX];
int c[MAX];
int sz[MAX];

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

    cin >> n >> q;
    for(int i=0;i<n;++i) c[i]=i;
    for(int i=0;i<n;++i) sz[i]=1;
    while(q--){
        int t; cin >> t;
        if(t==1){
            char ch; int a, b; cin >> a >> b >> ch; a--; b--;
            assert(ch=='a');
            int aa=a, bb=b;
            while(aa!=c[aa]) aa=c[aa];
            while(bb!=c[bb]) bb=c[bb];
            //cout << a << ", " << b << ": " << aa << ", " << bb << " <-> " << sz[aa] << " " << sz[bb] << '\n';
            //for(int i=0;i<n;++i) cout << c[i] << ' ';
            //cout << endl;
            //for(int i=0;i<n;++i) cout << sz[i] << ' ';
            //cout << endl << "--------------------------" << endl;
            if(sz[aa]<sz[bb]){c[a]=b; sz[bb]+=sz[aa];}
            else{c[b]=a; sz[aa]+=sz[bb];}
            //for(int i=0;i<n;++i) cout << c[i] << ' ';
            //cout << endl;
            //for(int i=0;i<n;++i) cout << sz[i] << ' ';
            //cout << endl << "==========================" << endl;
        }
        if(t==2){
            cin >> start >> target; start--; target--;
            //for(int i=0;i<n;++i) cout << c[i] << ' ';
            //cout << endl;
            while(c[start]!=start) start=c[start];
            while(c[target]!=target) target=c[target];
            if(start==target) cout << 1 << '\n';
            else cout << 0 << '\n';
        }
    }


    return 0;
}