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

import           Control.Monad.ST
import           Data.Array.IArray
import           Data.Array.MArray
import           Data.Array.ST
import           Data.Array.Unboxed
import           Data.Sequence         (Seq, ViewL (..), (><))
import qualified Data.Sequence         as S

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

bfs :: Int -> Array Int [Int] -> UArray Int Int
bfs n grp = runSTUArray $ do
    d <- newArray (1, n) (-1) :: ST s (STUArray s Int Int)
    writeArray d 1 0
    go (S.singleton 1) d
    return d
  where
    go q d
      | EmptyL  <- S.viewl q = return ()
      | x :< xs <- S.viewl q = do
            xd <- readArray d x
            vs <- filterM (fmap (== -1) . readArray d) (grp ! x)
            forM_ vs $ \v -> writeArray d v (xd + 1)
            go (xs >< S.fromList vs) d

main :: IO ()
main = do
    n <- readLn
    xss <- fmap (fmap readi . B.words) . B.lines <$> B.getContents
    let f [i, 0]   = (i, [])
        f (i:_:is) = (i, is)
    let grp = array (1, n) (fmap f xss) :: Array Int [Int]
    let dst = bfs n grp
    forM_ [1 .. n] $ \i -> do
        putStrLn . unwords $ fmap show [i, dst ! i]

