using System;
using System.Collections.Generic;
using System.Text;

class Program
{

    public void Proc()
    {
        int nodeCount = int.Parse(Console.ReadLine());
        this.PreOrderText = Console.ReadLine();
        this.InOrderText = Console.ReadLine();
        // testcode
        //this.PreOrderText = "1 2 3 4";
        //this.InOrderText = "1 2 3 4";
        string[] prestr = this.PreOrderText.Split(' ');
        string[] instr = this.InOrderText.Split(' ');

        this.PreOrder = new int[nodeCount];
       
        for(int i=0; i<nodeCount; i++) {
             this.PreOrder[i] = int.Parse(prestr[i]);
             this.PreOrderDic.Add(this.PreOrder[i], i);
             this.InOrder.Add(int.Parse(instr[i]));
        }

        TreeNd ansData = this.SetTree(this.InOrder);
        string ansText = GetPostorderText(ansData);
        Console.WriteLine(ansText);
    }

    public string GetPostorderText(TreeNd dat)
    {
        if (dat == null)
        {
            return string.Empty;
        }
        StringBuilder sb = new StringBuilder();
        if (dat.LeftChild != null)
        {
            string tmp = this.GetPostorderText(dat.LeftChild);
            if (!string.IsNullOrEmpty(tmp))
            {
                sb.Append(tmp);
            }
        }
        if (dat.RightChild != null)
        {
            string tmp = this.GetPostorderText(dat.RightChild);
            if (!string.IsNullOrEmpty(tmp))
            {
                if (sb.Length > 0)
                {
                    sb.Append(" ");
                }
                sb.Append(tmp);
            }
        }
        if (sb.Length > 0)
        {
            sb.Append(" ");
        }
        sb.Append(dat.NodeId);
        return sb.ToString();
    }

    private int[] PreOrder;
    private List<int> InOrder = new List<int>();
    private Dictionary<int, int> PreOrderDic = new Dictionary<int, int>();

    private String PreOrderText = string.Empty;
    private string InOrderText = string.Empty;

    private string GetInorderText(TreeNd dat)
    {
        StringBuilder sb = new StringBuilder();
        string left = string.Empty;
        if (dat.LeftChild != null)
        {
            left = this.GetInorderText(dat.LeftChild);
        }
        if (!string.IsNullOrEmpty(left))
        {
            sb.Append(left);
        }
        if (sb.Length > 0)
        {
            sb.Append(" ");
        }
        sb.Append(dat.NodeId);
        string right = string.Empty;
        if (dat.RightChild != null)
        {
            right = this.GetInorderText(dat.RightChild);
        }
        if (!string.IsNullOrEmpty(right))
        {
            sb.Append(" ");
            sb.Append(right);
        }
        return sb.ToString();
    }

    private int GetRootTreeId(List<int> list)
    {
        int minId = -1;
        int minIdx = int.MaxValue;
        foreach (int num in list)
        {
            if (this.PreOrderDic[num] < minIdx)
            {
                minIdx = this.PreOrderDic[num];
                minId = num;
            }
        }
        return minId;
    }

    private TreeNd SetTree(List<int> list){
        if (list == null || list.Count <= 0)
        {
            return null;
        }
        if (list.Count == 1)
        {
            return new TreeNd(list[0]);
        }
        int rootId = this.GetRootTreeId(list);
        int rootIndex = list.IndexOf(rootId);
        TreeNd rootNode = new TreeNd(rootId);

        List<int> left = new List<int>();
        List<int> right = new List<int>();
        for (int i = 0; i < rootIndex; i++)
        {
            left.Add(list[i]);
        }
        for (int i = rootIndex + 1; i < list.Count; i++)
        {
            right.Add(list[i]);
        }
        rootNode.LeftChild = this.SetTree(left);
        rootNode.RightChild = this.SetTree(right);
        if (rootNode.LeftChild != null)
        {
            rootNode.LeftChild.Parent = rootNode;
        }
        if (rootNode.RightChild != null)
        {
            rootNode.RightChild.Parent = rootNode;
        }
        return rootNode;
    }

    public class TreeNd:ICloneable
    {
        public TreeNd Parent;
        public int NodeId;
        public TreeNd LeftChild;
        public TreeNd RightChild;


        public TreeNd(int nodeid)
        {
            this.NodeId = nodeid;
        }

        public TreeNd SearchById(int id)
        {
            TreeNd ret = null;
            if (id == this.NodeId)
            {
                ret = this;
            }
            else if(this.LeftChild != null)
            {
                ret = this.LeftChild.SearchById(id);
            }
            if (ret == null && this.RightChild != null)
            {
                ret = this.RightChild.SearchById(id);
            }
            return ret;
        }

        public object Clone()
        {
            TreeNd tn = new TreeNd(this.NodeId);
            if (this.LeftChild != null)
            {
                tn.LeftChild = (TreeNd)this.LeftChild.Clone();
                tn.LeftChild.Parent = tn;
            }
            if (this.RightChild != null)
            {
                tn.RightChild = (TreeNd)this.RightChild.Clone();
                tn.RightChild.Parent = tn;
            }

            return tn;
        }
    }


    static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}

Measured memory_bytes: 9772