import sys class UnionFind(object): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.rank[x] < self.rank[y]: x, y = y, x if self.rank[x] == self.rank[y]: self.rank[x] += 1 self.par[y] = x self.size[x] += self.size[y] def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] N, Q = map(int, input().split()) query = [None] * Q U = UnionFind(N) for i in range(Q): query[i] = list(map(int, input().split())) if query[i][0] == 1: u, v = query[i][1:] u, v = u - 1, v - 1 U.union(u, v) else: if U.get_size(0) == N: query[i].append(-1) else: query[i].append(1) p, q = 0, -1 flag = 1 U = UnionFind(N) for i in range(Q): if query[i][0] == 2: if query[i][-1] == -1: flag = 0 else: if flag: u, v = query[i][1:] u, v = u - 1, v - 1 p = u q = v for i in range(Q): if query[i][0] == 2: continue else: u, v = query[i][1:] u, v = u - 1, v - 1 if u == p and v == q: break else: U.union(u, v) ans = [] for i in range(Q - 1, -1, -1): if query[i][0] == 2: if query[i][-1] != -1: u = query[i][1] - 1 if U.is_same(u, p): ans.append(q + 1) else: ans.append(p + 1) else: ans.append(-1) ans.reverse() for a in ans: print(a)