import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import Data.Maybe
import qualified Data.Sequence as Sq

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    [n,m] <- getIntList
    ab <- replicateM (n-1+m) $ do
        [a,b] <- getIntList
        return (a-1,b-1)

    din' <- VUM.replicate n (0 :: Int)
    edge' <- VM.replicate n ([] :: [Int])
    pars' <- VUM.replicate n (0 :: Int)
    forM_ ab $ \(a,b) -> do
        VM.modify edge' (b:) a
        VUM.modify din' succ b
    edge <- V.freeze edge'

    (v0:_) <- filterM (fmap (== 0) . VUM.read din') [0..n-1] 

    let go Sq.Empty = return ()
        go ((p,v) Sq.:<| sq) = do
            VUM.write pars' v (p+1)
            let nexts = edge V.! v
            forM_ nexts $ \i -> do
                VUM.modify din' pred i
            nxsq <- filterM (fmap (==0) . VUM.read din') nexts
            let sq' = foldl' (Sq.|>) sq $ map (\x -> (v,x)) nxsq
            go sq'
    go (Sq.singleton (-1,v0))
    pars <- VU.freeze pars'
    VU.mapM_ print pars