{-# LANGUAGE FlexibleContexts #-}

module Main where

import Debug.Trace
import Data.List
import qualified Data.Sequence as S
import qualified Data.ByteString.Char8 as B
import Data.Int
import Data.Char
import Data.Array
-- import Data.Array.IO
import Data.Maybe
import qualified Data.Map as M
import Control.Monad.ST
import Data.STRef
import Data.Ord
import Data.Functor
import Control.Applicative
import Control.Monad
import Text.Printf
    
solve :: Int -> [[Int]] -> [(Int, (Int, Int))]
solve n lst = toList (search initLog 0) where
    toList (_, l) = tail $ assocs l
    search log x
        | fvt log == 0 = recordB (foldl search (recordF log) (vertices!x))
        | otherwise = log where
            fvt (t, l) = fst (l!x)
            recordB (t, l) = (t+1, l // [(x, (fst (l!x), t))]) 
            recordF (t, l) = (t+1, l // [(x, (t, 0))])
    vertices = array (0, n) [(x, xs)| (x:_:xs) <- (0:n:[1..n]):lst]
    initLog = (0, listArray (0, n) $ (replicate (n+1) (0, 0)))

readIntLn:: IO Int
readIntLn = (fst . fromJust . B.readInt) <$> B.getLine

readIntList:: IO [Int]
readIntList = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

print' :: (Int, (Int, Int)) -> IO ()
print' (v, (f, b))= printf "%d %d %d\n" v f b

main :: IO() 
main = do
    n <- readIntLn
    l <- replicateM n readIntList
    mapM_ print' $ solve n l

