class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * n def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def unite(self, x, y): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) # 公式解説より # ルートのうち、最小番号の頂点と最大番号の頂点を管理 # uniteするたびに、それぞれを更新 # 非連結頂点は、そこから出力すればいい N, Q = map(int, input().split()) UF = UnionFind(N+1) smallest, largest = 1, N for i in range(Q): query = list(map(int, input().split())) if query[0] == 1: u, v = query[1], query[2] UF.unite(u, v) if UF.same(u, smallest) == True: j = 1 while smallest+j <= N: if UF.find(smallest+j) == j: smallest += j break else: j += 1 if UF.same(u, largest) == True: j = 1 while largest - j >= 1: if UF.find(largest-j) == j: largest -= j break else: j += 1 elif query[0] == 2: v = query[1] if UF.size(v) == N: print(-1) else: if UF.same(v, smallest) == True: print(largest) else: print(smallest) #print('smallest', smallest, 'largest', largest)