module Main where

import Control.Monad
    (foldM_)
import Control.Applicative
    ((<$>))

type Query = [Int]

main :: IO ()
main = do
     nq <- take 2 <$> fmap read <$> words <$> getLine :: IO [Int]
     let (n, q) = (nq !! 0, nq !! 1)
     let set = fromList $ take n [0..]
     qs <- take q <$> fmap (fmap read . words) <$> lines <$> getContents :: IO [Query]
     let actions = queryToAction qs
     foldM_ (\set action -> action set) set actions
     
queryToAction :: [Query] -> [DisjointSet -> IO DisjointSet]
queryToAction qs = do
    q <- qs
    let (c, i, j) = (q !! 0, q !! 1, q !! 2)
    if c == 0
        then
            return $ unite i j
        else
            return $ same i j

unite :: Int -> Int -> DisjointSet -> IO DisjointSet
unite i j set = return $ union set i j

same :: Int -> Int -> DisjointSet -> IO DisjointSet
same i j set = do
    if connected set i j then putStrLn "1" else putStrLn "0"
    return set

-- 

data DisjointSetElement = Refer Int | Root
        deriving (Show, Read)

type DisjointSet = [DisjointSetElement]

fromList :: [Int] -> DisjointSet
fromList = map (\ _ -> Root)

findRoot :: DisjointSet -> Int -> Int
findRoot set n = f x
        where
            x = set !! n
            f :: DisjointSetElement -> Int
            f Root = n
            f (Refer m) = findRoot set m

rearrange :: DisjointSet -> Int -> DisjointSet
rearrange set n = f x
            where
                x = set !! n
                f :: DisjointSetElement -> DisjointSet
                f Root = set
                f (Refer m) = rearrange updated m
                    where
                        root = findRoot set n
                        updated = update n (Refer root) set
                        
findRoot' :: DisjointSet -> Int -> (Int, DisjointSet)
findRoot' set n = f x
            where
                x = set !! n
                f :: DisjointSetElement -> (Int, DisjointSet)
                f Root = (n, set)
                f (Refer m) = (root, updated)
                    where
                        (root, s) = findRoot' set m
                        updated = update n (Refer root) s

connected :: DisjointSet -> Int -> Int -> Bool
connected set i j = (findRoot set i) == (findRoot set j)

union :: DisjointSet -> Int -> Int -> DisjointSet
union set n m
    | i /= j = update j (Refer i) set
    | otherwise = set
    where
        i = findRoot set n
        j = findRoot set m


update :: Int -> a -> [a] -> [a]
update n y xs = [if i == n then y else x | (i, x) <- zip [0..] xs]