import Control.Monad
import Data.List

initial_state :: [[Int]]
initial_state = []

find' :: Int -> [[Int]] -> Maybe [Int]
find' x [] = Nothing
find' x (st:sts)
    | elem x st = Just st
    | otherwise = find' x sts

drop' :: Int -> Int -> [[Int]] -> [[Int]]
drop' _ _ [[]] = [[]]
drop' _ _ [] = []
drop' x y (st:sts)
    | elem x st = sts
    | elem y st = sts
    | otherwise = st : (drop' x y sts)

merge :: Int -> Int -> Maybe[Int] -> Maybe[Int] -> [Int]
merge x y Nothing   Nothing   = [x,y]
merge x y (Just xx) Nothing   = (y:xx)
merge x y Nothing   (Just yy) = (x:yy)
merge x y (Just xx) (Just yy) = xx++yy

unite :: Int -> Int -> [[Int]] -> [[Int]]
unite x y state =
    let xs  = find' x state
        ys  = find' y state
        tmp = drop' x y state
    in
      (merge x y xs ys):tmp

same :: Int -> Int -> [[Int]] -> String
same x y state =
    let set = find' x state
    in
      if set == Nothing
      then "0"
      else
          let (Just s) = set
          in
            if elem y s
            then "1"
            else "0"

--action :: Int -> Int -> Int -> Int -> [[Int]] -> IO [[Int]]
action max code x y state =
  case code of
    0 -> do
      return $ state `seq` unite x y state
    1 -> do
      putStrLn $ same x y state
      return state

main = do
  c1 <- getLine
  let l1 = map read $ words c1 :: [Int]
      max = l1!!0
      state = initial_state
  c2 <- getContents
  let i0 = map words $ lines c2 
      i = map (map read) i0

  foldM_ (\st (i0:i1:i2:is) -> action max i0 i1 i2 st) state i