#include <bits/stdc++.h>

using namespace std;

#define Long long long
#define bint __int128
#define _3bkarm cin.tie(NULL); cout.tie(NULL); ios::sync_with_stdio(false);

const int mod = 998244353;

vector<int> p, root;
vector<int> treeSize;
vector< set<int> > adj;

int update;
void dfs(int v) {
    root[v] = update;
    for (int u : adj[v]) {
        if (u == p[v]) continue;
        p[u] = v, dfs(u);
    }
}

void get_shit_done() {
    int n, q;
    cin >> n >> q;

    p.assign(n + 1, -1);
    treeSize.assign(n + 1, 1);
    adj.assign(n + 1, {});
    root.assign(n + 1, -1);
    for (int v = 1; v <= n; ++v) root[v] = v;

    int x = 0;
    while (q--) {
        int a, b, c;
        cin >> a >> b >> c;

        a = 1 + (1LL * a * (1 + x) % mod) % 2;
        b = 1 + (1LL * b * (1 + x) % mod) % n;
        c = 1 + (1LL * c * (1 + x) % mod) % n;

        if (a == 1) {
            if ( treeSize[ root[b] ] < treeSize[ root[c] ] )
                swap(b, c);
            treeSize[ root[b] ] += treeSize[ root[c] ];
            p[c] = b;
            adj[b].insert(c);
            adj[c].insert(b);
            update = root[b], dfs(c);
        } else {
            if (p[b] == p[c] and p[b] != -1) x = p[b];
            else if ( adj[b].count(p[c]) ) x = p[c];
            else if ( adj[c].count(p[b]) ) x = p[b];
            else x = 0;
            cout << x << '\n';
        }
    }
}

signed main() {
    _3bkarm

    int ts = 1;
//    cin >> ts;
    while (ts--) {
        get_shit_done();
    }

    return 0;
}