#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
using namespace std;
int n, q;
bool moguce_doci(vector<list<int> >& graf, int a, int b){
    queue<int> q;
    q.push(a);
    vector<bool> posecen(n + 1, false);
    while(!q.empty()){
        int cvor = q.front();
        q.pop();
        if(cvor == b)
            return true;
        posecen[cvor] = true;
        for(const auto& sused : graf[cvor]){
            if(!posecen[sused]){
                q.push(sused);
            }
        }
    }
    return false;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> q;
    vector<list<int> > graf(n + 1);
    while(q--){
        int tip, a, b;
        char c;
        cin >> tip >> a >> b;
        if(tip == 1){
            graf[a].push_back(b);
            graf[b].push_back(a);
            cin >> c;
        }
        else if(tip == 2){
            if(moguce_doci(graf, a, b))
                cout << "1" <<endl;
            else
                cout << "0" <<endl;
        }
    }
    return 0;

}