import sys tokens = sys.stdin.readline().strip().split() n = int(tokens[0]) m = int(tokens[1]) g = range(n) hist = [] def root(x): global g rootIndex = x while g[rootIndex] != rootIndex: rootIndex = g[rootIndex] g[x] = rootIndex return rootIndex for line in sys.stdin: tokens = line.strip().split() if tokens[0] == '1': a = int(tokens[1]) b = int(tokens[2]) r = root(b) hist.append(r) g[r] = a elif tokens[0] == '2': count = int(tokens[1]) while count > 0: r = hist.pop() g[r] = r count -= 1 elif tokens[0] == '3': a = int(tokens[1]) b = int(tokens[2]) print 1 if root(a) == root(b) else 0