import Control.Applicative
import Data.Array.IArray
import Data.Array.ST 
import Control.Monad
import Control.Monad.ST
import Debug.Trace
import Data.List

type Graph = Array Int [Int]
type STResult s = STArray s Int (Int, Int)
type Result = Array Int (Int, Int)

main :: IO ()
main = do
  n <- readLn :: IO Int
  ns <- replicateM n $ do
    (a:b:ns) <- getInts
    return (a, ns)
  let graph = makeGraph n ns
  mapM_ putStrLn $ res2str $ solve n graph

getInts :: IO [Int]
getInts = map read . words <$> getLine

makeGraph :: Int -> [(Int, [Int])] -> Graph
makeGraph n = accumArray (++) [] (1, n)

res2str :: Result -> [String]
res2str res = zipWith (curry f) [1..] res' 
  where
    res' :: [(Int, Int)]
    res' = map (res !) [1..(snd (bounds res))]
    f (a, (b, c)) = show a ++ " " ++
                    show b ++ " " ++ show c

solve :: Int -> Graph -> Result
solve n graph = runST $ do
  res <- newArray (1, n) (-1, -1) :: ST s (STResult s)
  solve' 1 2 [] res
  where
    solve' m d path res = do
      res'<- dfs m d path res
      let (flg, n', d') = check res'
      if flg
        then return res'
        else do
        res'' <- thaw res'
        solve' n' (d'+2) [] res''
      where
        dfs :: Int -> Int -> [Int] ->
               STResult s -> ST s Result
        dfs current depth ls _res
          | depth /= d && null ls = do
              writeArray _res m (d-1, depth)
              freeze _res
          | otherwise = do
              cs <- filterM (\i -> do
                                (s, _) <- readArray _res i
                                return (s == -1)) is
              if null cs
                then do
                (s', _) <- readArray _res current 
                writeArray _res current (s', depth)
                if null ls
                  then 
                  freeze _res
                  else
                  dfs (head ls) (depth + 1) (tail ls) _res
                else do
                let c = head cs
                writeArray _res c (depth, 0)
                dfs c (depth + 1) (current:ls) _res
                  where
                    is = graph ! current
    check :: Result -> (Bool, Int, Int)
    check res = (flg, n', d')
      where
        is = map (\i -> let (s, e) = res ! i in
                          (i, s, e) ) [1..n]
        (_, _, d') = maximumBy (\(_, _, c1) (_, _, c2) ->
                           compare c1 c2) is
        (n', _, _) = minimumBy (\(n1, _, _) (n2, _, _) ->
                       compare n1 n2) $
                     filter (\(_, s1, _) -> s1 == -1) is
        flg = all (\(n, s, e) -> s /= -1) is
        
        