#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int main()
{
    ios_base::sync_with_stdio(false);
//    cin.tie(0);
    int n; cin >> n;
    vector< vector<pair<int, char>> > g(n, vector<pair<int, char>>());
    int q; cin >> q;
    while(q--){
        int tip, u, v;
        cin >> tip >> u >> v; u--; v--;
        if(tip == 1){
            char c;
            cin >> c;
            g[u].push_back({v, c});
            g[v].push_back({u, c});
        }else{
            stack<int> dfs;
            dfs.push(u);
            bool moguce = false;
            vector<bool> poseceni(n);
            while(!dfs.empty()){
                int t = dfs.top();
                dfs.pop();
                if(poseceni[t]) continue;
                poseceni[t] = 1;
                if(t == v){
                    moguce = true;
                    break;
                }
                for(auto par : g[t]){
                    dfs.push(par.first);
                }
            }
            cout << moguce << "\n";
        }
    }
}