class BinTree
  attr_accessor :node
  def initialize(n)
    @node = Array.new(n){Node.new()}
  end
  def preorder(n=root())
    str = ""
    return str if n < 0
    #root
    str << sprintf(" %s", n)
    #left
    str << preorder(node[n].left)
    #right
    str << preorder(node[n].right)
    return str
  end
  def inorder(n=root())
    str = ""
    return str if n < 0
    #left
    str << inorder(node[n].left)
    #root
    str << sprintf(" %s", n)
    #right
    str << inorder(node[n].right)
    return str
  end
  def postorder(n=root())
    arr = []
    return [] if n < 0
    #left
    arr += postorder(node[n].left)
    #right
    arr += postorder(node[n].right)
    arr += [n]
  end
  def root
    #find root
    r = 0
    node.size.times {|i|
      if node[i].parent == -1 then
        r = i
        break
      end
    }
    return r
  end
  def add(i, l, r)
    node[i].left = l
    node[i].right = r
    node[l].parent = i unless l == -1
    node[r].parent = i unless r == -1
  end
  def order_to_tree(preorder, inorder)
    n = preorder.size
    i = preorder[0]
    idx_root = inorder.index(i)
    if idx_root == 0 then
      inorder_left = []
    else
      inorder_left = inorder[0..idx_root-1]
    end
    inorder_right = inorder[idx_root+1..n-1]
     idx = preorder.index(inorder_right[0])
    if idx == nil then
      preorder_left = preorder[1..n-1]
      preorder_right = []
    else
      preorder_left = preorder[1..idx-1]
      preorder_right = preorder[idx..n-1]
    end

    if inorder_left.size == 1 then
      node[i].left = inorder_left[0]
      node[inorder_left[0]].parent = i
    end
    if inorder_right.size == 1 then
      node[i].right = inorder_right[0]
      node[inorder_right[0]].parent = i
    end
    if inorder_left.size > 1 then
      node[i].left = self.order_to_tree(preorder_left, inorder_left)
      node[node[i].left].parent = i
    end    
    if inorder_right.size > 1 then
      node[i].right = self.order_to_tree(preorder_right, inorder_right)
      node[node[i].right].parent = i
    end
    return i
  end
end

class Node
  attr_accessor :parent, :left, :right
  def initialize
    @parent = -1
    @left = -1
    @right = -1
  end
end

n = gets.to_i
tree = BinTree.new(n)

preorder = gets.split.map(&:to_i).map{|i| i -= 1}
inorder = gets.split.map(&:to_i).map{|i| i -= 1}

tree.order_to_tree(preorder, inorder)

puts tree.postorder().map{|i| i += 1}.join(" ")