#include <bits/stdc++.h>

using namespace std;

#define MAXN 1010

long long n,q;
vector<pair<pair<long long,long long>,char> > upiti;
vector<long long> adj[MAXN];
bool pos[MAXN];

void dfs(long long node)
{
    pos[node]=true;
    for (long long sled:adj[node])
    {
        if (pos[sled]==false) dfs(sled);
        else continue;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n>>q;
    long long bra=0;
    for (long long i=1;i<=q;i++)
    {
        long long t;cin>>t;
        if (t==1)
        {
            long long a,b;cin>>a>>b;
            char c;cin>>c;
            upiti.push_back({{a,b},c});
            if (c=='a') bra++;
        }
        if (t==2)
        {
            long long a,b;cin>>a>>b;bra++;
            upiti.push_back({{a,b},' '});
        }
    }
    if (bra==q)
    {
        ///1.subtask
        for (long long i=0;i<q;i++)
        {
            long long a=upiti[i].first.first;
            long long b=upiti[i].first.second;
            char c=upiti[i].second;
            if (c!=' ') {adj[a].push_back(b);adj[b].push_back(a);}
            else
            {
                for (long long i=1;i<=n;i++) pos[i]=false;
                dfs(a);
                if (pos[b]) cout<<1<<endl;
                else cout<<0<<endl;
            }
        }
    }
}