#include<bits/stdc++.h>

#define pb push_back
#define fi first
#define se second
#define mp make_pair

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;

template <typename T> chkmin(T& a,T b){return a>b?a=b,1:0;}
template <typename T> chkmax(T& a,T b){return a<b?a=b,1:0;}

ll readint(){
    ll x=0,f=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int n,q;
int op[300005],u[300005],v[300005],tt[1005][1005],fa[1005*1005],res[1005][1005];
char ch[300005],cc[1005][1005];
vector<pii> g[1005][30];
vector<int> my[1005*1005];

int id(int i,int j){return (i-1)*n+j;}

int main(){
    n=readint(); q=readint();
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            tt[i][j]=1e9;
            cc[i][j]='!';
        }
    }
    for(int i=1;i<=q;i++){
        op[i]=readint();
        if(op[i]==1){
            u[i]=readint();
            v[i]=readint();
            if(u[i]>v[i]) swap(u[i],v[i]);
            ch[i]=getchar();
            while(ch[i]<'a'||ch[i]>'z') ch[i]=getchar();
            tt[u[i]][v[i]]=i;
            cc[u[i]][v[i]]=ch[i];
        }else{
            u[i]=readint();
            v[i]=readint();
            if(u[i]>v[i]) swap(u[i],v[i]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(tt[i][j]>q) continue;
            g[i][cc[i][j]-'a'].pb({tt[i][j],j});
            g[j][cc[i][j]-'a'].pb({tt[i][j],i});
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<26;j++){
            sort(g[i][j].begin(),g[i][j].end());
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            res[i][j]=1e9;
            fa[id(i,j)]=i;
            my[id(i,j)].pb(id(i,j));
            //if(i==j) res[id(i,j)]=0,ch[id].clear();
        }
    }
    set<array<int,3>> st;
    /*for(int i=1;i<=n;i++){
        for(int j=i;j<=n;j++){
            if(i==j) continue;
            for(int c=0;c<26;c++){
                if(g[i][c].empty()||g[j][c].empty()) continue;

            }
        }
    }*/
    for(int i=1;i<=q;i++){
        if(op[i]==2) continue;
        res[u[i]][v[i]]=i;
        st.insert({i,u[i],v[i]});
    }
    while(!st.empty()){
        auto it=st.begin();
        int x=(*it)[1],y=(*it)[2];
        st.erase(it);
        for(int c=0;c<26;c++){
            for(auto gx:g[x][c]){
                for(auto gy:g[y][c]){
                    int dd=max({res[x][y],gx.fi,gy.fi});
                    int i=gx.fi,j=gy.se;
                    if(i>j) swap(i,j);
                    if(res[i][j]>dd){
                        if(res[i][j]<=q) st.erase({res[i][j],i,j});
                        res[i][j]=dd;
                        st.insert({res[i][j],i,j});
                    }
                }
            }
        }
    }
    for(int i=1;i<=q;i++){
        if(op[i]==1) continue;
        if(res[u[i]][v[i]]<=i) printf("1\n");
        else printf("0\n");
    }
    return 0;
}