#include #include using namespace std; using namespace atcoder; using ll = long long; using mint = modint998244353; using vi = vector; using vvi = vector; using vvvi = vector; using vll = vector; using vvll = vector; using vvvll = vector; using vmi = vector; using vvmi = vector; using vvvmi = vector; #define all(a) (a).begin(), (a).end() #define rep2(i, m, n) for (int i = (m); i < (n); ++i) #define rep(i, n) rep2(i, 0, n) #define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i) #define drep(i, n) drep2(i, n, 0) class UnionFind{ public: vector m_parent; vector m_sizes; set ls; UnionFind(int n){ rep(i, n)m_parent.push_back(i); rep(i, n)m_sizes.push_back(1); rep(i, n)ls.insert(i); } int find(int i) { if (m_parent[i] == i) { return i; } return (m_parent[i] = find(m_parent[i])); } void merge(int a, int b){ a = find(a); b = find(b); if(a != b){ if(m_sizes[a] < m_sizes[b])swap(a, b); m_sizes[a] += m_sizes[b]; ls.erase(b); m_parent[b] = a; } } bool same(int a, int b){ return (find(a) == find(b)); } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int n, q; cin >> n >> q; UnionFind uf(n); while(q--){ int i; cin >> i; if(i == 1){ int u, v; cin >> u >> v; uf.merge(u-1, v-1); }else{ int u; cin >> u; u--; int l = uf.find(u); if(uf.m_sizes[l] == n){ cout << -1 << endl; continue; } auto it = uf.ls.begin(); if(*it == l)it++; cout << *it + 1 << endl; } } return 0; }