class DSU: def __init__(self, n): self.parent = list(range(n)) self.rank = [0] * n def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) return self.parent[x] def union(self, x, y): rootX = self.find(x) rootY = self.find(y) if rootX != rootY: if self.rank[rootX] > self.rank[rootY]: self.parent[rootY] = rootX elif self.rank[rootX] < self.rank[rootY]: self.parent[rootX] = rootY else: self.parent[rootY] = rootX self.rank[rootX] += 1 def process_queries(N, Q, queries): dsu = DSU(N) connected = [{} for _ in range(N)] next_unconnected = [0] * N results = [] for query in queries: if query[0] == 1: u, v = query[1] - 1, query[2] - 1 if dsu.find(u) != dsu.find(v): dsu.union(u, v) elif query[0] == 2: v = query[1] - 1 root_v = dsu.find(v) while next_unconnected[v] < N and (next_unconnected[v] == v or dsu.find(next_unconnected[v]) == root_v): next_unconnected[v] += 1 if next_unconnected[v] >= N: results.append('-1') else: results.append(str(next_unconnected[v] + 1)) return "\n".join(results) import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) Q = int(data[idx + 1]) idx += 2 queries = [] for _ in range(Q): qtype = int(data[idx]) if qtype == 1: u = int(data[idx + 1]) v = int(data[idx + 2]) queries.append((1, u, v)) idx += 3 elif qtype == 2: v = int(data[idx + 1]) queries.append((2, v)) idx += 2 print(process_queries(N, Q, queries))