import sys input = sys.stdin.readline N,Q=map(int,input().split()) # UnionFind Group = [i for i in range(N+1)] # グループ分け Nodes = [1]*(N+1) # 各グループのノードの数 def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): if Nodes[find(x)] < Nodes[find(y)]: Nodes[find(y)] += Nodes[find(x)] Nodes[find(x)] = 0 Group[find(x)] = find(y) else: Nodes[find(x)] += Nodes[find(y)] Nodes[find(y)] = 0 Group[find(y)] = find(x) QS=[list(map(int,input().split())) for i in range(Q)] for i in range(Q): if len(QS[i])==2: continue com,u,v=QS[i] if com==1: if find(u)!=find(v): if Nodes[find(u)]+Nodes[find(v)]==N: last=i break else: Union(u,v) ANS=[-1]*(N+1) for i in range(N+1): if find(i)!=1: no_one=i for i in range(N+1): if find(i)==1: ANS[i]=no_one else: ANS[i]=1 for i in range(Q): if len(QS[i])==3: continue com,u=QS[i] if com==2: if i>=last: print(-1) else: print(ANS[u])