#include <iostream>
#include <bits/stdc++.h>

using namespace std;

vector<string> f(int v, vector<vector<pair<int, char>>> &a){
    vector<string> s(a.size(), "");

    queue<int> q;
    q.push(v);
    vector<bool> vis(a.size(), false);

    while(!q.empty()){
        int curr = q.front();
        q.pop();
        vis[curr] = true;

        for(int i = 0; i < a[curr].size(); i++){
            if(!vis[a[curr][i].first]){
                vis[a[curr][i].first] = true;
                q.push(a[curr][i].first);
                s[a[curr][i].first] = s[curr] + (a[curr][i].second);
            }
        }
    }

    return s;
}

bool is(string s){
    for(int i = 0; i < s.length()/2+1; i++){
        if(s[i] != s[s.length()-i-1]) return false;
    }

    return true;
}

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

    int n, q;
    cin >> n >> q;
    vector<vector<pair<int, char>>> a(n, vector<pair<int, char>> (0));

    for(int i = 0; i < q; i++){
        int t;
        cin >> t;

        if(t == 1){
            int u, v;
            char c;
            cin >> u >> v >> c;
            u--; v--;

            a[u].push_back(make_pair(v, c));
            a[v].push_back(make_pair(u, c));
        }
        else{
            int u, v;
            cin >> u >> v;
            u--; v--;

            vector<string> s = f(u, a);

            if(is(s[v])) cout << 1 << endl;
            else cout << 0 << endl;
        }
    }
}