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

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

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

bfs :: IOUArray Int Int -- distance
    -> Array Int [Int]  -- graph
    -> IO ()
bfs d g = do
    forM_ (zip [0..] (walk [[1]])) $ \(i, vs) -> do
        forM_ vs $ \v -> do
            writeArrayIf (== -1) d v i
  where
    walk :: [[Int]] -> [[Int]]
    walk ([]:_) = []
    walk (vs:vss) = vs : walk (xs : vss)
      where
        xs = concatMap (g !) vs

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
    distance <- newArray (1, n) (-1)

    bfs distance arr

    forM_ [1 .. n] $ \i -> do
        d <- readArray distance i
        putStrLn . unwords $ fmap show [i, d]

