#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 100000000000000LL
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[maxq];
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0);
    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;
    cin >> n >> q;
    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});
            dp[0][x][y] = dp[0][y][x] = i;
        }else{
            cin >> x >> y;
            Q[i] = {x,y};
        }
    }

    for(ll i = 1;i<=q;i++){
        if(Q[i][0]==1) continue;
        ll x = Q[i][1],y = Q[i][2];
        if(x==y) cout<<1<<endl;
        else if(dp[0][x][y]<=i) cout<<1<<endl;
        else cout<<0<<endl;
    }
    return 0;
}