import Data.List
import Data.Function
import Data.Maybe
import Debug.Trace

data Node = Node Int deriving (Show, Eq, Ord)
type Edge = (Node, Node)
type Distance = (Node, Int)

maxD = 101

getId :: Node -> Int
getId (Node i) = i

getNodes :: Int -> [Edge] -> [Node]
getNodes n edges = sort $ map snd . filter (\(s,e) -> getId s == n) $ edges

visit :: [Edge] -> [Distance] -> [Distance] -> [Distance]
visit _     []    distances  = distances
visit edges stack distances = let neighbors           = getNodes (getId lastNode) edges
                                  neighborsDistances  = map (\x -> (x, d+1)) . filter (\node-> not (visited node distances)) $ neighbors
                                  updatedStack        = foldr (:) remain neighborsDistances
                                  updatedDistances    = mergeDistances distances neighborsDistances
                              in (visit edges updatedStack updatedDistances)
  where (remain, (lastNode, d)) = (init stack, last stack)

mergeDistances :: [Distance] -> [Distance] -> [Distance]
mergeDistances xs ys = nub $ (doMerge xs ys) ++ (doMerge ys xs)
  where doMerge []     _            = []
        doMerge (x:xs) newDistances = (minDist x newDistances) : (doMerge xs newDistances)
        minDist (Node i1, d1) distances = let dist = find (\(Node i, d) -> i == i1) distances
                                          in (Node i1, min d1 (maybe maxD (getId . fst) dist))

visited :: Node -> [Distance] -> Bool
visited node distances = node `elem` (map fst distances)

getDistance :: Node -> [Distance] -> Maybe Distance
getDistance node distances = find (\(n, d) -> node == n) distances

parseEdge :: [String] -> [Edge]
parseEdge (s:_:xs) = map (\e -> (Node (read s), Node (read e))) xs

listNodeDistances :: Int -> [Distance] -> [Distance]
listNodeDistances n distances = sortBy (compare `on` (getId . fst)) . map (\i -> if visited (Node i) distances then fromJust (getDistance (Node i) distances) else (Node i, -1)) $ [1..n]

format :: [Distance] -> [String]
format []     = []
format ((Node i, d):ds) = (show i ++ " " ++ show d) : (format ds)

main = do
  n <- fmap (read) getLine
  inputs <- fmap (map words . lines) $ getContents
  let edges = concatMap parseEdge inputs
  mapM_ putStrLn (format . listNodeDistances n $ visit edges [(Node 1, 0)] [(Node 1, 0)])