#include <bits/stdc++.h>

using namespace std;

vector<int> adj[1000];
bool visited[1000];
void yo(int u, int v) {
        visited[u] = true;
        for(int i = 0; i < adj[u].size(); i++) {
           if(visited[adj[u][i]] == false) {
            yo(adj[u][i], v);
           }
        }
}
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);
        }
        else {
                yo(u, v);
            if(visited[v] == true) cout << 1 << "\n";
            else cout << 0 << "\n";
            }
            memset(visited, false, 1000);
    }
    return 0;
}