import qualified Control.Applicative as A

main = do
    _ <- getLine
    p <- map read . words A.<$> getLine
    i <- map read . words A.<$> getLine
    putStrLn $ unwords $ map show $ solve p i

type Node = Int
type Nodes = [Node]
data Tree = Branch Nodes Tree Tree | Empty

solve :: Nodes -> Nodes -> Nodes
solve p i = postOrder t
    where (_, t) = reconstruct p (Branch i Empty Empty)

postOrder :: Tree -> Nodes
postOrder Empty = []
postOrder (Branch n l r) = postOrder l ++ postOrder r ++ n

reconstruct :: Nodes -> Tree -> (Nodes, Tree)
reconstruct [] t = ([], t)
reconstruct (p:ps) t = reconstruct ps (splitBranch p t)

splitBranch :: Node -> Tree -> Tree
splitBranch p t@(Branch n Empty Empty) =
    if p `elem` n
       then let (l, r) = splitNodes n p
                in (Branch [p] (Branch l Empty Empty) (Branch r Empty Empty))
       else t
splitBranch p (Branch n l r) = (Branch n (splitBranch p l) (splitBranch p r))

splitNodes :: Nodes -> Node -> (Nodes, Nodes)
splitNodes [] p = ([], [])
splitNodes (n:ns) p =
    if n == p
       then ([], ns)
       else let (l, r) = splitNodes ns p in ((n:l), r)
