#include <bits/stdc++.h>
#define pc pair<int, string>
#define f first
#define s second

using namespace std;

bool is(string s)
{
    int n = s.length();
    int i =0, j = n-1;
    while(i<j)
    {
        if(s[i++]!=s[j--])return false;
    }
    return true;
}

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    int n, q;cin>>n>>q;
    int moze[n+5][n+5] = {{0}};

    vector< vector< pc > >g(n+5);

    while(q--)
    {
        int qq;cin>>qq;
        if(qq==1)
        {
            int a, b;
            string s;
            cin>>a>>b>>s;
            moze[a][b]=1;
            moze[b][a]=1;
            for(int j = 1; j<=n; j++)
            {
                if(moze[j][a] || moze[a][j]){
                    moze[j][b]=1;
                    moze[b][j]=1;
                }
            }

        }else
        {
            int a, b;cin>>a>>b;
            cout<<(moze[a][b] || moze[b][a])<<"\n";

        }
    }
    return 0;
}