#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 1010;
int n, q, par[N], sz[N];
int a[N], dp[N][N], o[N][N];
int root(int v) {
    if (par[v] == v)
        return v;
    return par[v] = root(par[v]);
}
struct gas {
    int t, u, v, c;
};
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int tt = 1;
    //cin >> tt;
    while (tt--) {
        cin >> n >> q;
        for (int i = 1;i <= n;i++) {
            par[i] = i;
            sz[i] = 1;
        }
        vector<gas> qs;
        int ok = 1;
        for (int i = 1;i <= q;i++) {
            int t;
            cin >> t;
            if (t == 1) {
                int u, v;
                char c;
                cin >> u >> v >> c;
                a[v] = c;
                ok &= (c == 'a');
                qs.push_back({t, u, v, c - 'a'});
            }
            else {
                int u, v;
                cin >> u >> v;
                qs.push_back({t, u, v, i});
            }
        }
        if (ok) {
            for (int i = 0;i < q;i++) {
                int t, u, v, c;
                t = qs[i].t;
                u = qs[i].u;
                v = qs[i].v;
                c = qs[i].c;
            if (t == 1) {
                int x = root(u);
                int y = root(v);
                if (sz[x] < sz[y])
                    swap(x, y);
                par[y] = x;
                sz[x] += sz[y];
            }
            else {
                int u, v;
                u = root(u);
                v = root(v);
                if (u == v)
                    cout << "1\n";
                else
                    cout << "0\n";
            }
        }
        }
        else {
            for (int i = 1;i <= n;i++) {
            dp[i][i] = 1;
            if (i < n && a[i] == a[i + 1])
                dp[i][i + 1] = 1, o[i][i + 1] = 1;
        }
        for (int i = 3;i <= n;i++) {
            for (int l = 1, r = i;r <= n;r++, l++) {
                if (o[l + 1][r]) {
                    dp[l][r] = (a[l] == a[l + 1]);
                }
                else {
                    if (dp[l + 1][r - 1] && a[l] == a[r])
                        dp[l][r] = 1;
                }
            }
        }
        for (int i = 0;i < q;i++) {
                int t, u, v, c;
                t = qs[i].t;
                u = qs[i].u;
                v = qs[i].v;

                c = qs[i].c;
            if (t == 1) {
                int x = root(u);
                int y = root(v);
                if (sz[x] < sz[y])
                    swap(x, y);
                par[y] = x;
                sz[x] += sz[y];
            }
            else {
                if (u == 1)
                    ++u;
                if (u > v)
                    swap(u, v);
                cout << dp[u][v] << '\n';
            }
        }
    }
    }
    return 0;
}