def reconstruct preorder, inorder
  root = preorder.shift
  unless root_idx = inorder.index(root)
    reconstruct(preorder,inorder)
    return
  end

  reconstruct(preorder,inorder[0...root_idx]) unless root_idx==0
  reconstruct(preorder,inorder[root_idx+1..-1]) unless root_idx==inorder.size-1
  $postorder << root
end

gets
Pre=gets.split.map &:to_i
In=gets.split.map &:to_i
$postorder=[]
reconstruct(Pre,In)
puts $postorder.join " "