{-# OPTIONS_GHC -O #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ViewPatterns #-}

import Data.Char
import Data.List
import Data.Maybe
import Control.Monad
import Control.Monad.ST
import qualified Data.ByteString.Char8 as B
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as M
import qualified Data.Sequence as S
import Data.Sequence ((|>), (<|), ViewL(..), ViewR(..))

vl (n:<_) = n
vr (_:>n) = n

f a [x,y] = a==x || a==y
g a b xy = f a xy || f b xy

pick a b vs =
    case V.findIndex (g a b) vs of
        Nothing -> Nothing
        Just i  -> Just $ split i vs

split i vs = runST $ do
    v' <- V.unsafeThaw vs
    M.swap v' 0 i
    rv <- V.unsafeFreeze v'
    return (V.head rv, V.tail rv)

solve :: S.Seq Int -> V.Vector [Int] -> S.Seq Int
solve path vs =
    case pick lh rh vs of
        Nothing -> path
        Just ([a,b], v) | lh==a -> solve (b<|path) v
        Just ([a,b], v) | lh==b -> solve (a<|path) v
        Just ([a,b], v) | rh==a -> solve (path|>b) v
        Just ([a,b], v) | rh==b -> solve (path|>a) v
    where
        lh = vl $ S.viewl path
        rh = vr $ S.viewr path

main = do
    [n,m] <- unfoldr (B.readInt.B.dropWhile isSpace) <$> B.getLine
    [a,b] <- unfoldr (B.readInt.B.dropWhile isSpace) <$> B.getLine
    v <- V.replicateM (m-1) $ unfoldr (B.readInt.B.dropWhile isSpace) <$> B.getLine
    putStrLn $ tail $ foldl' (\s i -> s ++" "++ show i) "" $ solve (S.fromList [a,b]) v
