{-# LANGUAGE FlexibleContexts #-}

module Main where

-- import Debug.Trace
import Data.List
import qualified Data.Sequence as S
import qualified Data.ByteString.Char8 as B
import Data.Int
import Data.Char
import Data.Array
-- import Data.Array.IO
import Data.Maybe
import qualified Data.Map as M
import Control.Monad.ST
import Data.STRef
import Data.Ord
import Data.Functor
import Control.Applicative
import Control.Monad
import Text.Printf
    
solve :: Int -> [[Int]] -> [(Int, Int)]
solve n lst = assocs (bfs 1 [[1]] verticesTable) where
    bfs _ [] tbl = tbl
    bfs d ps tbl = bfs (d+1) ps' (tbl // updates) where
        (ps', updates) = iter [] [] ps where
            iter ac bc [] = (ac, bc)
            iter ac bc (el@(e:es):ps) = iter ac' bc' ps where
                newVertices = filter (\x -> tbl!x < 0) (adjacentsList!e)
                ac' = (map (:el) newVertices) ++ ac
                bc' = [(x, d) | x <- newVertices] ++ bc

    adjacentsList = array (1, n) [(x, xs)| (x:_:xs) <- lst]
    verticesTable = accumArray (+) (-1) (1, n) [(1,1)]

readIntLn:: IO Int
readIntLn = (fst . fromJust . B.readInt) <$> B.getLine

readIntList:: IO [Int]
readIntList = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

print' :: (Int, Int) -> IO ()
print' (v, d)= printf "%d %d\n" v d

main :: IO() 
main = do
    n <- readIntLn
    l <- replicateM n readIntList
    mapM_ print' $ solve n l

