#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;
    vector< vector<bool> > postoji(n, vector<bool>(n));
    while(q--){
        int tip, u, v;
        cin >> tip >> u >> v; u--; v--;
        if(tip == 1){
            char c;
            cin >> c;
            for(int i = 0; i < n; i++){
                if(postoji[u][i]){
                    postoji[i][v] = 1;
                    postoji[v][i] = 1;
                }
                if(postoji[v][i]){
                    postoji[u][i] = 1;
                    postoji[i][u] = 1;
                }
            }
            postoji[u][v] = 1;
            postoji[v][u] = 1;
            g[u].push_back({v, c});
            g[v].push_back({u, c});
        }else{
            cout << postoji[u][v] << "\n";
        }
    }
}