object Main extends App {
  val n: Int = io.StdIn.readInt()
  val preorder: Vector[Int] = io.StdIn.readLine().split(" ").map(_.toInt).toVector
  val inorder: Vector[Int] = io.StdIn.readLine().split(" ").map(_.toInt).toVector
  val postorder = toPostOrder(inorder)
  println(postorder.mkString(" "))

  def toPostOrder(l: Vector[Int]): Vector[Int] = l match {
    case Vector() | Vector(_) => l
    case _ => {
      val p: Int = l.indices.minBy(i => preorder.indexOf(l(i)))
      val left: Vector[Int] = l.take(p)
      val right: Vector[Int] = l.drop(p+1)
      toPostOrder(left) ++ toPostOrder(right) ++ Vector(l(p))
    }
  }
}
