#include <bits/stdc++.h>

using namespace std;
vector< vector< int > > adj;
vector<bool> vis;
int c;
int dfs(int x){
    vis[x]=true;
    if(x==c) return true;
    bool ind=0;
    for(auto y : adj[x]){
        if(!vis[y]) ind=ind|dfs(y);
    }
    return ind;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n, q, t, u, v;
    char ca;
    cin>>n>>q;
    adj.resize(n);
    while(q--){
        cin>>t>>u>>v;
        if(t==1){
            cin>>ca;
            adj[u-1].push_back(v-1);
            adj[v-1].push_back(u-1);
        }
        else{
            vis.resize(n, false);
            c=v-1;
            cout<<dfs(u-1)<<'\n';
        }
    }
    return 0;
}