#include <bits/stdc++.h>

#define ll long long
#define pii pair<int, int>
#define xx first
#define yy second
#define pb push_back
#define MAXN 1010
#define MAXQ 300010
#define INF 1000000000000010LL

using namespace std;

int n, t[MAXQ], u[MAXQ], v[MAXQ];
string s[MAXQ];
bool bio[MAXN];
vector<int> g[MAXN];

void dfs(int u)
{
    bio[u]=true;
    for (auto v:g[u])
    {
        if (!bio[v]) dfs(v);
    }
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int q;
    cin>>n>>q;

    bool prvisub=true, drugisub=true;
    for (int i=0; i<q; i++)
    {
        cin>>t[i]>>u[i]>>v[i];
        if (t[i]==1) { cin>>s[i]; if (s[i]!="a") prvisub=false; }
    }

    if (prvisub)
    {
        for (int i=0; i<q; i++)
        {
            if (t[i]==1)
            {
                g[u[i]].pb(v[i]);
                g[v[i]].pb(u[i]);
            }
            else
            {
                for (int i=1; i<=n; i++) bio[i]=false;
                dfs(u[i]);
                cout<<bio[v[i]]<<endl;
            }
        }
    }
    else assert(0);
}