class UnionFind: def __init__(self, n): self.node = [-1 for _ in range(n)] def root(self, v): if self.node[v] < 0: return v st = [] while self.node[v] >= 0: st.append(v) v = self.node[v] for u in st: self.node[u] = v return v def size(self, v): v = self.root(v) return (- self.node[v]) def same(self, u, v): return self.root(u) == self.root(v) def unite(self, u, v): ru = self.root(u) rv = self.root(v) if ru == rv: return du = self.node[ru] dv = self.node[rv] if du <= dv: self.node[rv] = ru self.node[ru] += dv else: self.node[ru] = rv self.node[rv] += du n, q = map(int, input().split()) queries = [list(map(int, input().split())) for _ in range(q)] uf = UnionFind(n) cnt = n connect = 1 << 60 for i in range(q): t, *args = queries[i] if t == 1: u, v = args u -= 1 v -= 1 if uf.root(u) != uf.root(v): if cnt == 2: connect = i break uf.unite(u, v) cnt -= 1 root = {} for v in range(n): r = uf.root(v) root[r] = 0 vs = list(root) for i in range(len(vs)): root[vs[i]] = vs[(i + 1) % len(vs)] for i in range(q): t, *args = queries[i] if t == 2: if i > connect: print("-1") else: v = args[0] - 1 print(root[uf.root(v)] + 1)