#include <bits/stdc++.h>
using namespace std;
int N, Q, T, u, v;
char c;
char A[1006][1006];
struct ParentRanking
{
    int parent;
    int ranking;
}DSU[1006];
inline void Setup()
{
    for(int i = 1; i <= N; i++) DSU[i].parent = i;
    return;
}
inline int FIND(int X)
{
    while(X != DSU[X].parent)
    {
        X = DSU[X].parent;
        DSU[X].parent = DSU[DSU[X].parent].parent;
    }
    return X;
}
inline void Union(int u, int v)
{
    u = FIND(u);
    v = FIND(v);
    if(DSU[u].ranking > DSU[v].ranking)
    {
        DSU[v].parent = u;
        return;
    }
    if(DSU[u].ranking < DSU[v].ranking)
    {
        DSU[u].parent = v;
        return;
    }
    if(DSU[u].ranking == DSU[v].ranking)
    {
        DSU[v].parent = u;
        DSU[u].ranking++;
        return;
    }
    return;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin >> N >> Q;
    Setup();
    while(Q--)
    {
        cin >> T;
        if(T == 1)
        {
            cin >> u >> v >> c;
            A[u][v] = c;
            A[v][u] = c;
            Union(u, v);
        }
        else
        {
            cin >> u >> v;
            printf("%d\n", FIND(u) == FIND(v));
        }
    }

    return 0;
}