gets
preorder = gets.chomp.split.map(&:to_i)
inorder = gets.chomp.split.map(&:to_i)

def rec(n, inorder, preorder)
    if inorder != []
        c = preorder[n]; n += 1
        x = inorder.index(c)
        left = inorder[0...x]
        right = inorder[(x+1)..-1]
        n = rec(n, left, preorder)
        n = rec(n, right, preorder)
        print "#{c} "
    end
    n
end

rec(0, inorder, preorder)
puts
