module Dsu : sig
    type t

    val make   : int -> t
    val merge  : t -> int -> int -> int
    val same   : t -> int -> int -> bool
    val leader : t -> int -> int
    val size   : t -> int -> int
end = struct
    type t = int * int array

    let make n =
        n, Array.make n (-1)

    let rec leader ((n, parent_or_size) as dsu) a =
        if parent_or_size.(a) < 0 then a else
            let r = leader dsu parent_or_size.(a) in
            let () = parent_or_size.(a) <- r in
            r

    let merge ((n, parent_or_size) as dsu) a b =
        let x = leader dsu a in
        let y = leader dsu b in
        if x = y then x else (
            let x, y = if -parent_or_size.(x) < -parent_or_size.(y) then y, x else x, y in
            parent_or_size.(x) <- parent_or_size.(x) + parent_or_size.(y);
            parent_or_size.(y) <- x;
            x
        )

    let same dsu a b =
        leader dsu a = leader dsu b

    let size ((n, parent_or_size) as dsu) a =
        -parent_or_size.(leader dsu a)
end;;

Scanf.scanf "%d %d" (fun n q ->
    let dsu = Dsu.make n in
    for i = 1 to q do
        Scanf.scanf " %d %d %d" (fun t u v ->
            if t = 0 then ignore (Dsu.merge dsu u v) else
                Printf.printf "%d\n" @@ if Dsu.same dsu u v then 1 else 0
        )
    done
)