class UnionFind: def __init__(self, n): self.data = [-1] * (n + 1) self.nexts = [-1] * (n + 1) # 次の要素(なければ-1) self.tails = list(range(n + 1)) # 末尾の要素 def root(self, a: int) -> int: if self.data[a] < 0: return a self.data[a] = self.root(self.data[a]) return self.data[a] def unite(self, a: int, b: int) -> bool: pa = self.root(a) pb = self.root(b) if pa == pb: return False if self.data[pa] > self.data[pb]: pa, pb = pb, pa self.data[pa] += self.data[pb] # pa を pb をつなげる self.data[pb] = pa # pa の末尾に pb を繋げる self.nexts[self.tails[pa]] = pb self.tails[pa] = self.tails[pb] return True def issame(self, a: int, b: int) -> bool: return self.root(a) == self.root(b) def size(self, a: int) -> int: """a が属する集合のサイズ""" return -self.data[self.root(a)] def group(self, a): """a が属する集合""" v = self.root(a) while v != -1: yield v v = self.nexts[v] N, Q = map(int, input().split()) leaders = set(range(1, N+1)) uf = UnionFind(N+10) for _ in range(Q): t, *a = map(int, input().split()) if t == 1: u, v = a u = uf.root(u) v = uf.root(v) uf.unite(u, v) r = uf.root(u) if r != u: leaders.discard(u) if r != v: leaders.discard(v) elif t == 2: v = a[0] r = uf.root(v) for x in leaders: if r != uf.root(x): print(x) break else: print(-1)