import qualified Data.Array.IArray as IA
import Data.Array.ST
import Control.Monad
import Control.Applicative
import Control.Monad.ST

type Graph = IA.Array Int [Int]
type Result = IA.Array Int Int
type STResult s = STArray s Int Int

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

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

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

res2str :: Result -> [String]
res2str res = map (\i ->
                     show i ++ " " ++ show (res IA.! i))
              [1..snd (IA.bounds res)]

solve :: Int -> Graph -> Result
solve n graph = runSTArray $ do
  res <- newArray (1, n) (n+1) 
  bfs 1 0 res
  return res
  where
    bfs :: Int -> Int -> STResult s -> ST s Int
    bfs current depth res
      | depth == (n+1) = return current
      | otherwise = do
          writeArray res current depth
          cs <- filterM
                (\i -> do
                    s <- readArray res i
                    return (s > depth)) $ graph IA.! current
          if null cs
            then return current
            else do
            mapM_ (\i -> bfs i (depth+1) res) cs
            return current