local n, q = io.read("*n", "*n")
local parent = {}
for i = 1, n do parent[i] = i end

local function uf_findroot(idx)
  local idx_update = idx
  while parent[idx] ~= idx do
    idx = parent[idx]
  end
  while parent[idx_update] ~= idx do
    parent[idx_update], idx_update = idx, parent[idx_update]
  end
  return idx
end

for i = 1, q do
  local t, u, v = io.read("*n", "*n", "*n")
  local ru, rv = uf_findroot(u), uf_findroot(v)
  if t == 0 then
    parent[rv], parent[v] = ru, ru
  else
    print(ru == rv and 1 or 0)
  end
end
