#include<algorithm>
#include<bitset>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

#define dbg(x) (cout<<#x<<" = "<<(x)<<'\n')
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif

typedef long long int lld;
typedef pair<int, int> PII;
typedef pair<int, lld> PIL;
typedef pair<lld, int> PLI;
typedef pair<lld, lld> PLL;

const int INF = (1LL << 31) - 1;
const lld LINF = (1LL << 62) - 1;
const int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int MOD = (int)(1e9) + 7;

const int NMAX = 100000 + 5;
const int MMAX = 100000 + 5;
const int KMAX = 100000 + 5;
const int PMAX = 100000 + 5;
const int LMAX = 100000 + 5;
const int VMAX = 100000 + 5;

#define DIM 10000
char buff[DIM];
int position = 0;

void read(int &nr) {
    nr = 0;
    while(buff[position] < '0' || buff[position] > '9')
        if(++position == DIM)
            fread(buff, 1, DIM, stdin), position = 0;
    while('0' <= buff[position] && buff[position] <= '9') {
        nr = nr * 10 + buff[position] - '0';
        if(++position == DIM)
            fread(buff, 1, DIM, stdin), position = 0;
    }
}

int N, M, OP;
int root[NMAX];
vector<pair<int, PII> > S, P;
int sol[NMAX];

int find(int x) {
    if(x != root[x]) {
        S.push_back(make_pair(OP, make_pair(x, root[x])));
        root[x] = find(root[x]);
    }
    return root[x];
}

void unite(int x, int y) {
    P.push_back(make_pair(OP, make_pair(x, root[x])));
    root[x] = y;
}

int main() {
    int i, a, b, t, x, y;

#ifdef HOME
    freopen(inputFile.c_str(), "r", stdin);
    freopen(outputFile.c_str(), "w", stdout);
#endif

    read(N);
    read(M);

    for(i = 1; i <= N; i++)
        root[i] = i;

    for(i = 1, OP = 0; i <= M; i++) {
        read(t);

        if(t == 1) {
            read(x);
            read(y);
            ++OP;
            unite(find(x), find(y));
            continue;
        }

        if(t == 2) {
            read(x);
            while(x--) {
                while(!S.empty() && S.back().first == OP) {
                    a = S.back().second.first;
                    b = S.back().second.second;
                    root[a] = b;
                    S.pop_back();
                }

                while(!P.empty() && P.back().first == OP) {
                    a = P.back().second.first;
                    b = P.back().second.second;
                    root[a] = b;
                    P.pop_back();
                }

                OP--;
            }
            continue;
        }

        if(t == 3) {
            read(x);
            read(y);
            printf("%d\n", (find(x) == find(y)));
            continue;
        }
    }

    return 0;
}