#include <bits/stdc++.h>

using namespace std;

vector<int> adj[1005];
bool ff[1001][1001];

void yo(int u, int v) {
        ff[u][v] = true;
        ff[v][u] = true;
        for(int i = 0; i < adj[u].size(); i++) {
           if(ff[v][adj[u][i]] == false) {
            yo(adj[u][i], v);
           }
        }
        for(int i = 0; i < adj[v].size(); i++) {
           if(ff[u][adj[v][i]] == false) {
            yo(adj[v][i], u);
           }
        }
}
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    int n, q;
    cin >> n >> q;
    char c;
    for(int i = 0; i < q; i++) {
        int t, u, v;
        cin >> t >> u >> v;
        if(t == 1) {
           cin >> c;
            adj[u].push_back(v);
            adj[v].push_back(u);
            yo(u, v);
        }
        else {

            if(ff[v][u] == true) cout << 1 << "\n";
            else cout << 0 << "\n";
            }
    }
    return 0;
}