#include using namespace std; using ll = long long; using Vi = std::vector ; using Vl = vector; using VVi = vector; class UnionFind { Vi par; Vi siz; Vl val; // VVi iv; public : set st; UnionFind(int N = 0) { par.resize(N); siz.resize(N); // iv.resize(N); iota(par.begin(), par.end(), 0); fill(siz.begin(), siz.end(), 0); for (int i = 0; i < N; i ++) { // iv[i].push_back(i); st.insert(i); } } void init(int N) { par.resize(N); siz.resize(N); iota(par.begin(), par.end(), 0); fill(siz.begin(), siz.end(), 0); } int root(int v) { if (v == par[v]) { return v; } else { return par[v] = root(par[v]); } } bool unit(int a, int b) { a = root(a); b = root(b); if (a == b) { return false; } if (siz[a] < siz[b]) { swap(a, b); } par[b] = a; siz[a] += siz[b]; /*for (auto x : iv[b]) { iv[a].push_back(x); }*/ st.erase(b); return true; } }; int main () { int N, Q; cin >> N >> Q; UnionFind uu(N); while (Q--) { int t; cin >> t; if (t - 1) { int a; cin >> a; if (uu.st.size() == 1) { puts("-1"); } else { a = uu.root(--a); if (a != *uu.st.begin()) { cout << (*uu.st.begin()) + 1 << endl; } else { cout << (*uu.st.rbegin()) + 1 << endl; } } } else { int a, b; cin >> a >> b; uu.unit(--a, --b); } } }