#include using namespace std; struct dsu { public: dsu() : _n(0) {} dsu(int n) : _n(n), parent_or_size(n, -1) { for(int i = 0; i < _n; i++){ leader_set.insert(i); } } int merge(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); int x = leader(a), y = leader(b); if (x == y) return x; if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y); leader_set.erase(y); parent_or_size[x] += parent_or_size[y]; parent_or_size[y] = x; return x; } bool same(int a, int b) { assert(0 <= a && a < _n); assert(0 <= b && b < _n); return leader(a) == leader(b); } int leader(int a) { assert(0 <= a && a < _n); if (parent_or_size[a] < 0) return a; return parent_or_size[a] = leader(parent_or_size[a]); } int get(int a) { assert(0 <= a && a < _n); for(auto &v : leader_set){ if(!same(v, a))return v + 1; } return -1; } private: int _n; // root node: -1 * component size // otherwise: parent std::set leader_set; std::vector parent_or_size; }; int main(){ int N, Q, cmd, u, v; cin >> N >> Q; dsu uf(N); while(Q--){ cin >> cmd; if(cmd == 1){ cin >> u >> v; uf.merge(--u, --v); }else{ cin >> v; cout << uf.get(--v) << '\n'; } } }