import Control.Applicative
import Control.Monad
import Control.Monad.State
import Data.Array.IO

data DisjointSet = DS { parent :: Int
                      , rank :: Int
                      } deriving (Show)

type DisjointForest = IOArray Int DisjointSet

main = do
        [n,q] <- map read.words<$>getLine
        qs <- replicateM q $ map read.words<$>getLine
        dsf <- newListArray (0,n-1) $ map (\x -> DS x 0) [0..n-1]
        execStateT (sequence_ $ map inpToQuery qs) dsf

inpToQuery [c,x,y]
    | c == 0 = unite x y
    | c == 1 = same x y

modifyArray :: Ix i => IOArray i a -> i -> (a -> a) -> IO ()
modifyArray arr ix f = do
        e <- readArray arr ix
        writeArray arr ix (f e)

makeSet :: Int -> StateT DisjointForest IO ()
makeSet x = get >>= \arr -> liftIO $ modifyArray arr x (\_ -> DS x 0)


findSet :: Int -> StateT DisjointForest IO Int
findSet x = do
        f <- get
        x' <- liftIO $ parent<$>readArray f x
        if x == x' then return x
                   else findSet x'

findSet' :: [Int] -> Int -> StateT DisjointForest IO Int
findSet' acc x = do
        f <- get
        x' <- liftIO $ parent<$>readArray f x
        if x == x' then do
                    liftIO $ mapM_ (\a -> modifyArray f a (\ds -> ds {parent = x'})) acc
                    return x'
                   else findSet' (x:acc) x'

union :: Int -> Int -> StateT DisjointForest IO ()
union x y = do
        f <- get
        liftIO $ modifyArray f x (\ds -> ds {parent = y})
        liftIO $ modifyArray f x (\ds -> ds {rank = rank ds + 1})

union' x y = do
        f  <- get
        x' <- findSet' [] x
        y' <- findSet' [] y
        liftIO $ unionIO f x' y'

unionIO arr x y = do
        rankX <- rank<$>readArray arr x
        rankY <- rank<$>readArray arr y
        case rankX `compare` rankY of
            GT -> modifyArray arr y (\ds -> ds {parent = x})
            EQ -> modifyArray arr x (\ds -> ds {parent = y, rank = rank ds+1})
            LT -> modifyArray arr x (\ds -> ds {parent = y})


same :: Int -> Int -> StateT DisjointForest IO ()
same x y = do
        x' <- findSet' [] x
        y' <- findSet' []  y
        f <- get
        if x' == y' then liftIO $ putStrLn "1"
                    else liftIO $ putStrLn "0"

unite :: Int -> Int -> StateT DisjointForest IO ()
unite x y = do
        x' <- findSet' [] x
        y' <- findSet' [] y
        union' x' y'