let split str delim =
  let open String in
  let rec doit s acc =
    match
      try Some (rindex s delim) with _ -> None
    with
    | None -> s :: acc
    | Some i -> doit (sub s 0 i) (sub s (i + 1) (length s - i - 1) :: acc) in
  doit str []

let solve n q =
  let a = Array.init n (fun i -> (i, 0)) in
  let rec find_set x =
    if x <> fst a.(x) then a.(x) <- (find_set (fst a.(x)), snd a.(x));
    fst a.(x) in
  let link x y =
    if snd a.(x) > snd a.(y) then a.(y) <- (x, snd a.(y))
    else begin
      a.(x) <- (y, snd a.(x));
      if snd a.(x) = snd a.(y) then a.(y) <- (fst a.(y), snd a.(y) + 1)
    end in
  let unite x y = link (find_set x) (find_set y) in
  let same_p x y = find_set x = find_set y in
  let rec doit i =
    if i < q then begin
      begin match List.map int_of_string (split (read_line ()) ' ') with
      | 0 :: x :: y :: _ -> unite x y
      | 1 :: x :: y :: _ -> print_endline (if same_p x y then "1" else "0")
      | _ -> ()
      end;
      doit (i + 1)
    end in
  doit 0

let () =
  match List.map int_of_string (split (read_line ()) ' ') with
  | [n; q] -> solve n q
  | _ -> ()