#include <bits/stdc++.h>

#define ll long long
#define loop(i, a, b) for (ll i = a; i < b; i++)

using namespace std;

ll dsu_find(vector<ll> &dsu, ll x)
{
    if (dsu[x] == x)
        return x;
    return dsu[x] = dsu_find(dsu, dsu[x]);
}

void dsu_merge(vector<ll> &dsu, ll a, ll b)
{
    a = dsu_find(dsu, a);
    b = dsu_find(dsu, b);

    dsu[a] = b;
}
void dsu_init(vector<ll> &dsu)
{
    loop(i, 0, dsu.size())
    {
        dsu[i] = i;
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    ll n, q, t, u, v;
    char c;
    cin >> n >> q;
    vector<ll> dsu(n);
    dsu_init(dsu);

    loop(i, 0, q)
    {
        cin >> t >> u >> v;
        if (t == 1)
        {
            cin >> c;
            dsu_merge(dsu, u, v);
        }
        else
        {
            if (dsu_find(dsu, u) == dsu_find(dsu, v))
            {
                cout << "1\n";
            }
            else
            {
                cout << "0\n";
            }
        }
    }

    return 0;
}