#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define pb push_back
#define pll pair<ll,ll>
#define si(a) ((ll)(a.size()))
#define fi first
#define sc second
#define here cerr<<"==============================\n"
#define llinf 100000000000000LL
using namespace std;
#define maxn 1005
#define maxq 300005
ll n,q;
vector<pll> g[27][maxn];
ll dp[maxn][maxn];
array<ll,4> Q[maxq];
priority_queue<array<ll,3> > pq;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cerr.tie(0);
    cin >> n >> q;
    for(ll i = 1;i<=n;i++) for(ll j = 1;j<=n;j++) dp[i][j] = llinf;
    for(ll i = 1;i<=n;i++) dp[i][i] = 0;
    if(n>100) return 0;
    for(ll i = 1;i<=q;i++){
        ll tip; cin >> tip;
        ll x,y; char c;
        if(tip==1){
            cin >> x >> y >> c;
            ll val = c-'a'+1;
            Q[i] = {tip,x,y,c-'a'+1};
            g[val][x].pb({y,i});
            g[val][y].pb({x,i});
            pq.push({-i,x,y});
            dp[x][y] = dp[y][x] = i;
        }else{
            cin >> x >> y;
            Q[i] = {tip,x,y,0};
        }
    }
    for(ll i = 1;i<=n;i++) pq.push({0,i,i});
    while(si(pq)){
        array<ll,3> p = pq.top(); pq.pop();
        ll x = p[1],y = p[2],cur = -p[0];
        if(cur!=dp[x][y]) continue;
        for(ll e = 1;e<=26;e++){
            for(pll px : g[e][x]){
                ll sx = px.fi;
                ll dx = px.sc;
                for(pll py : g[e][y]){
                    ll sy = py.fi;
                    ll dy = py.sc;
                    ll d = max({dx,dy,dp[x][y]});
                    if(d<dp[sx][sy]){
                        dp[sx][sy] = dp[sy][sx] = d;
                        pq.push({-d,sx,sy});
                    }
                }
            }
        }
    }
    for(ll i = 1;i<=q;i++){
        if(Q[i][0]==1) continue;
        ll x = Q[i][1],y = Q[i][2];
        if(dp[x][y]<=i) cout<<1<<endl;
        else cout<<0<<endl;
    }
    return 0;
}
/**
8 9
1 1 2 a
1 2 3 b
1 3 4 c
1 4 5 b
1 5 6 b
1 6 7 b
1 7 8 a
2 1 8
2 2 5
**/