#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using vi = vector; using vvi = vector; using vl = vector; using vvl = vector; using vb = vector; using vvb = vector; using vd = vector; using vs = vector; using pii = pair; using pll = pair; using pdd = pair; using vpii = vector; using vpll = vector; using vpdd = vector; const int inf = (1 << 30) - 1; const ll INF = 1LL << 60; const int MOD = 1000000007; //const int MOD = 998244353; struct UnionFind { vector par; vector sizes; UnionFind(int n) : par(n), sizes(n, 1) { for (int i = 0; i < n; i++) par[i] = i; } int root(int x) { if (x == par[x]) return x; return par[x] = root(par[x]); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (sizes[x] < sizes[y]) swap(x, y); par[y] = x; sizes[x] += sizes[y]; } bool same(int x, int y) { return root(x) == root(y); } int size(int x) { return sizes[root(x)]; } }; int main() { int n, q; cin >> n >> q; UnionFind uf(n); set node; for (int i = 0; i < n; i++) node.insert(i); for (int i = 0; i < q; i++) { int t, u, v; cin >> t; if (t == 1) { cin >> u >> v; u--; v--; if (!uf.same(u, v)) { int r1 = uf.root(u); int r2 = uf.root(v); uf.unite(u, v); if (uf.root(r1) != r1) node.erase(r1); if (uf.root(r2) != r2) node.erase(r2); } } else { cin >> v; v--; int r = uf.root(v); if (*node.begin() == r) { if (node.size() == 1) { cout << -1 << endl; } else { cout << *(next(node.begin())) + 1 << endl; } } else { cout << *node.begin() + 1 << endl; } } } return 0; }