#include <bits/stdc++.h>

using namespace std;

void addEdge(vector<int> adj[], int s, int d) {
    adj[s].push_back(d);
    adj[d].push_back(s);
}

void stampaC(vector<int> adj[], int startNode,vector<bool>& visited,int trazim,bool& nasao)
{
    queue<int> q;
    visited[startNode] = true;
    q.push(startNode);
    while (!q.empty()) {
        int currentNode = q.front();
        q.pop();
        for (auto neighbor : adj[currentNode]) {
            if (!visited[neighbor]) {
                visited[neighbor] = true;
                q.push(neighbor);
                if(neighbor==trazim){
                    nasao=true;
                }
            }
        }
    }
}

int main()
{
    std::ios_base::sync_with_stdio (false);
    int n,q,p1,p2,p3;
    //vector<bool> visited(2000,false);
    char rly;
    cin >> n >> q;
    bool nasao;
    vector<int> adj[n];
    for(int i=0;i<q;i++){
        cin >> p1 >> p2 >> p3;
        if(p1==1){
            cin >> rly;
            addEdge(adj,p2-1,p3-1);
        }
        else{
            vector<bool> visited(2000,false);
            nasao=false;
            stampaC(adj,p2-1,visited,p3-1,nasao);
            if(nasao)
                cout << 1 << endl;
            else
                cout << 0 << endl;
        }
    }
    return 0;
}