#include <bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;

int N, Q, u, v, k;
char c;
struct graf
{
    int x;
    char c;
};
bool P[1005];
vector<graf> VG[1005];
graf temp;

void DFS(int node, int e)
{
    P[node] = 1;
    for(int i = 0; i < VG[node].size(); i++)
        if(!P[VG[node][i].x])
            DFS(VG[node][i].x, e);
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> Q;
    while(Q--)
    {
        cin >> k;
        if(k == 1)
        {
            cin >> u >> v >> c;
            temp.c = c;
            temp.x = u;
            VG[v].pb(temp);
            temp.x = v;
            VG[u].pb(temp);
        }
        else
        {
            memset(P, 0, sizeof(P));
            cin >> u >> v;
            DFS(u, v);
            if(P[v])
                cout << 1 << "\n";
            else
                cout << 0 << "\n";
        }
    }
    return 0;
}