import           Control.Applicative
import           Control.Monad
import qualified Data.ByteString.Char8 as B

import           Data.Array.IArray
import           Data.Array.IO
import           Data.Array.MArray
import           Data.IORef

readi :: B.ByteString -> Int
readi b | Just (n, _) <- B.readInt b = n

dfs :: IOUArray Int Int -- discover
    -> IOUArray Int Int -- finished
    -> Array Int [Int]  -- graph
    -> IO ()
dfs d f g = do
    let (from, to) = bounds g
    t <- newIORef 0
    forM_ [from .. to] $ \i -> go t i
  where
    go :: IORef Int -> Int -> IO ()
    go ref k = do
        dv <- readArray d k
        when (dv == -1) $ do
            modifyIORef ref (+ 1)
            writeArrayIf (== -1) d k =<< readIORef ref
            mapM_ (go ref) (g ! k)
            modifyIORef ref (+ 1)
            writeArrayIf (== -1) f k =<< readIORef ref

writeArrayIf :: (MArray a e m, Ix i) => (e -> Bool) -> a i e -> i -> e -> m ()
writeArrayIf p a i e = do
    v <- readArray a i
    when (p v) $ writeArray a i e

main :: IO ()
main = do
    n <- readLn
    xss <- fmap (fmap readi . B.words) . B.lines <$> B.getContents

    let f [i, 0]   = (i, [])
        f (i:_:ks) = (i, ks)
    let arr = array (1, n) $ fmap f xss
    discover <- newArray (1, n) (-1)
    finished <- newArray (1, n) (-1)

    dfs discover finished arr

    forM_ [1 .. n] $ \i -> do
        a <- readArray discover i
        b <- readArray finished i
        putStrLn . unwords $ fmap show [i, a, b]

