#include <bits/stdc++.h>
#define endl '\n'
#define ll int
#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 1000000
using namespace std;
#define maxn 1005
#define maxq 300005
ll n,q;
vector<pll> g[27][maxn];
ll dp[27][maxn][maxn];
pll Q[maxq];
priority_queue<array<ll,4> > pq;
set<array<ll,4>> s;
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0);
    cin >> n >> q;
    for(ll e = 0;e<=26;e++) for(ll i = 1;i<=n;i++) for(ll j = 1;j<=n;j++) dp[e][i][j] = llinf;
    for(ll e = 0;e<=26;e++) for(ll i = 1;i<=n;i++) dp[e][i][i] = 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;
            g[val][x].pb({y,i});
            g[val][y].pb({x,i});
            s.insert({i,x,y,0});
            dp[0][x][y] = dp[0][y][x] = i;
        }else{
            cin >> x >> y;
            Q[i] = {x,y};
        }
    }
    for(ll i = 1;i<=n;i++) s.insert({0,i,i,0});
    while(si(s)){
        auto p = *s.begin(); s.erase(s.begin());
        ll x = p[1],y = p[2],e = p[3];
        if(e!=0){
            for(pll py : g[e][y]){
                ll sy = py.fi;
                ll dy = py.sc;
                ll d = max(p[0],dy);
                if(d<dp[0][x][sy]){
                    if(s.find({dp[0][x][sy],x,sy,0})!=s.end()) s.erase(s.find({dp[0][x][sy],x,sy,0}));
                    if(s.find({dp[0][x][sy],sy,x,0})!=s.end()) s.erase(s.find({dp[0][x][sy],sy,x,0}));
                    dp[0][x][sy] = dp[0][sy][x] = d;
                    s.insert({dp[0][x][sy],x,sy,0});
                }
            }
        }else{
            for(ll f = 0;f<2;f++){
                for(ll e = 1;e<=26;e++){
                    for(pll py : g[e][y]){
                        ll sy = py.fi;
                        ll dy = py.sc;
                        ll d = max(p[0],dy);
                        if(d<dp[e][x][sy]){
                            if(s.find({dp[e][sy][x],sy,x,e})!=s.end()) s.erase(s.find({dp[e][sy][x],sy,x,e}));
                            dp[e][sy][x] = d;
                            s.insert({dp[e][sy][x],sy,x,e});
                        }
                    }
                }
                swap(x,y);
            }
        }
    }
    for(ll i = 1;i<=q;i++){
        ll x = Q[i].fi,y = Q[i].sc;
        if(x==0) continue;
        if(x==y) cout<<1<<endl;
        else if(dp[0][x][y]<=i) cout<<1<<endl;
        else cout<<0<<endl;
    }
    return 0;
}