def reconstruct preorder, inorder
  no=inorder.size-1
  root = preorder.shift
  unless root_idx = inorder.index(root)#???
    $right_preorder << root
    return
  end
  if root_idx==0
  elsif root_idx==1
    $postorder << inorder[0]
  else
    reconstruct(preorder,inorder[0...root_idx])#???
  end

  if root_idx==no
  elsif root_idx==no-1
    $postorder << inorder[-1]
  else
    reconstruct($right_preorder.concat(preorder),inorder[root_idx+1..-1])#???
  end
  $postorder << root
end

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