#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define S second
#define F first
#define int long long

struct query{
    int u, v;
    char c;
};

const int N = 1001;
int p[N], sz[N];

int find(int v){
    if(p[v]==v)return v;
    return p[v]=find(p[v]);
}

void merge(int u, int v){
    u=find(u);
    v=find(v);
    if(u==v)return;
    if(sz[u]<sz[v])swap(u, v);
    p[v]=u;
    sz[u]+=sz[v];
}

void solve(){
    int n, q;
    cin >> n >> q;
    vector<query> v;
    bool ps=1;
    for(int i=0; i<q; i++){
        int t;
        cin >> t;
        if(t==1){
            int a, b;
            char c;
            cin >> a >> b >> c;
            v.pb({a, b, c});
            if(c!='a')ps=0;
        }
        else{
            int a, b;
            cin >> a >> b;
            v.pb({a, b, '?'});
        }
    }
    if(ps){
        for(int i=1; i<=n; i++){
            p[i]=i;
            sz[i]=1;
        }
        for(int i=0; i<q; i++){
            if(v[i].c=='?'){
                if(find(v[i].u)==find(v[i].v))cout << 1 << '\n';
                else cout << 0 << '\n';
            }
            else{
                merge(v[i].u, v[i].v);
            }
        }
        return;
    }
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
}