import java.io.PrintWriter import java.util.* import kotlin.math.* fun PrintWriter.solve() { val n = nextInt() val q = nextInt() val parents = mutableSetOf() val uf = UnionFind(n) (0 until n).forEach { parents.add(it) } for (i in 0 until q) { val t = nextInt() if (t == 1) { val u = nextInt() - 1 val v = nextInt() - 1 parents.remove(uf.find(u)) parents.remove(uf.find(v)) uf.unite(u, v) parents.add(uf.find(u)) } else { val v = nextInt() - 1 val v1 = parents.first() val v2 = parents.last() if (!uf.same(v, v1)) { println(v1 + 1) } else if (!uf.same(v, v2)) { println(v2 + 1) } else { println(-1) } } } } class UnionFind(n: Int) { private val rank = IntArray(n) { 0 } private val parent = IntArray(n) { -1 } fun find(x: Int): Int { if (parent[x] < 0) { return x } parent[x] = find(parent[x]) return parent[x] } fun unite(x: Int, y: Int) { val x1 = find(x) val y1 = find(y) if (x1 == y1) { return } if (rank[x1] < rank[y1]) { parent[y1] += parent[x1] parent[x1] = y1 } else { parent[x1] += parent[y1] parent[y1] = x1 if (rank[x1] == rank[y1]) { rank[x1]++ } } } fun same(x: Int, y: Int) = find(x) == find(y) fun size(x: Int) = -parent[find(x)] } fun main() { Thread(null, { val writer = PrintWriter(System.out, false) writer.solve() writer.flush() }, "solve", abs(1L.shl(26))) .apply { setUncaughtExceptionHandler { _, e -> e.printStackTrace(); kotlin.system.exitProcess(1) } } .apply { start() }.join() } // region Scanner private var st = StringTokenizer("") private val br = System.`in`.bufferedReader() fun next(): String { while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) return st.nextToken() } fun nextInt() = next().toInt() fun nextLong() = next().toLong() fun nextLine() = br.readLine() fun nextDouble() = next().toDouble() // endregion