import Control.Applicative ((<$>))
import Control.Monad
import Control.Monad.RWS.Strict
import qualified Data.IntMap as IM
import qualified Data.Set as S
import Data.List (sortBy)

type Time = Int

type AdjG = IM.IntMap [Int]
type State = (Time, IM.IntMap Bool)
type Log = S.Set (Bool, Int, Time)

main :: IO ()
main = do
  n <- readLn
  adjG <- fmap IM.fromList $ replicateM n $ do
    (u:_:vs) <- map read . words <$> getLine :: IO [Int]
    return (u,vs)
  let res = S.toList $ snd $ evalRWS (dfs n) adjG (1, IM.fromList $ zip [1..n] $ repeat False)

  forM_ [1..n] $ \i -> do
    putStrLn $ unwords $ map show $ (i:) $ map (\(_,_,t) -> t) $ sortBy (\(a,_,_) (b,_,_) -> a `compare` b) $ filter (\(_,v,_) -> v == i) res

dfs :: Int -> RWS AdjG Log State ()
dfs n = forM_ [1..n] $ \v -> do
  fls <- snd <$> get
  when (not $ fls IM.! v) $ rec v

rec :: Int -> RWS AdjG Log State ()
rec v = do
  (t, fls) <- get

  when (not $ fls IM.! v) $ do
    us <- asks (IM.!v)
    tell $ S.singleton (False, v, t)
    put (t+1, IM.alter (fmap (const True)) v fls)
    mapM_ rec us
    (t, fls) <- get
    tell $ S.singleton (True, v, t)
    put (t+1, fls)
    return ()