#include <bits/stdc++.h>
#define int long long

using namespace std;
/*
void printajniz(vector<int>a, int n, int x){
    //sort(a.begin(), a.end());
    for (int i=0;i<n;i++){
        for (int j=i;j<n;j++){
            int op1 = (a[i] & a[j]);
            int op2 = (a[i] | a[j]);
            a[i] = op1;
            a[j] = op2;
        }
    }
    for (auto t : a) cout <<t<<' ';
    cout <<'\n';
    cout <<a[x]<<'\n';
}

void solve(){
    int n; cin >> n;
    vector<int>a(n); for (auto &t : a) cin >> t;
    int q; cin >> q;
    while (q--){
        int t,k; cin >> t >> k;
        if (t == 0) printajniz(a,n,(n - k));
        else{
            int x; cin >> x;
            k--;
            for (int i=0;i<n;i++){
                int op1 = (a[i] & a[k]);
                int op2 = (a[i] | a[k]);
                if (op2 == x){
                    a[k] = op2;
                    a[i] = op1;
                    break;
                }
                else if (op1 == x){
                    a[k] = op1;
                    a[i] = op2;
                    break;
                }
            }
        }
    }
}



void solve(){
    int n; cin >> n;
    vector<int>a(n); for (auto &t : a) cin >> t;
    int q; cin >> q;
    while (q--){
        int t,k; cin >> t >> k;
        if (t == 0) printajniz(a,n,(n - k));
        else{
            int x; cin >> x;
            k--;
            for (int i=0;i<n;i++){
                int op1 = (a[i] & a[k]);
                int op2 = (a[i] | a[k]);
                if (op2 == x){
                    a[k] = op2;
                    a[i] = op1;
                    break;
                }
                else if (op1 == x){
                    a[k] = op1;
                    a[i] = op2;
                    break;
                }
            }
        }
    }
}

*/
map<pair<int, int>, char>mp;


bool ispalindrom(string s){
    int p1=0, p2=(int)s.size()-1;
    while (p1 < p2){
        if (s[p1] != s[p2]) return false;
        p1++; p2--;
    }
    return true;
}

string getpath(vector<int>&path, int x){
    int node = x;
    //cout <<node<<' ';
    vector<int>p;
    p.push_back(node);
    while (path[node] != -1){
        //cout <<path[node]<<' ';
        p.push_back(path[node]);
        node = path[node];
    }
    //cout <<'\n';
    string s = "";
    for (int i=1;i<(int)p.size();i++) s += mp[{p[i], p[i-1]}];
    return s;
}

bool bfs(vector<vector<pair<int, char>>>&g, int x, int y,int n){
    vector<bool>vis(n, false);
    queue<int>q;
    q.push(x);
    vis[x] = true;
    bool ok = false;
    vector<int>path(n, -1);
    while (!q.empty()){
        if (ok) break;
        auto t = q.front();
        q.pop();
        for (auto f : g[t]){
            if (vis[f.first]) continue;
            q.push(f.first);
            path[f.first] = t;
            vis[f.first] = true;
            if (f.first == y) {
                ok = true;
                break;
            }
        }
    }
    if (!ok) return false;
    string s = getpath(path, y);
    return ispalindrom(s);
}
/*
5 5
1 1 2 a
1 2 3 a
1 3 4 a
1 4 5 a
2 1 5
*/

void solve(){
    int n,q; cin >> n>> q;
    vector<vector<pair<int, char>>>g(n);


    while (q--){
        int op; cin >> op;
        if (op == 1){
            int a,b; cin >> a>> b;
            a--; b--;
            char c; cin >> c;
            g[a].push_back({b, c});
            g[b].push_back({a, c});
            mp[{a,b}] = c;
            mp[{b, c}] = c;
        }
        else{
            int a,b; cin >> a>> b; a--; b--;
            if (bfs(g, a, b, n)) cout <<"1\n";
            else cout <<"0\n";
        }
    }
}


int32_t main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);

    int t = 1; //cin >> t;
    while (t--){
        solve();
    }
    return 0;
}