class DSU: def __init__(self, n): self.parent = list(range(n)) self.rank = [1] * 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 = [set() for _ in range(N)] results = [] for query in queries: if query[0] == 1: u = query[1] - 1 v = query[2] - 1 dsu.union(u, v) connected[u].add(v) connected[v].add(u) elif query[0] == 2: v = query[1] - 1 found = False for i in range(N): if i != v and i not in connected[v] and dsu.find(i) != dsu.find(v): results.append(str(i + 1)) found = True break if not found: results.append('-1') return "\n".join(results) import sys input = sys.stdin.read data = input().split() N = int(data[0]) Q = int(data[1]) queries = [] index = 2 for _ in range(Q): query_type = int(data[index]) if query_type == 1: u = int(data[index + 1]) v = int(data[index + 2]) queries.append([1, u, v]) index += 3 elif query_type == 2: v = int(data[index + 1]) queries.append([2, v]) index += 2 print(process_queries(N, Q, queries))