#include <stdio.h>
#include <vector>
#include <list>
#include <map>
#include <array>
#include <memory>
#include <string>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <unordered_map>
#include <stdexcept>


using std::string;
using std::vector;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// util
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

std::vector<std::string> split(const std::string& input, char delimiter)
{
    std::istringstream stream(input);

    std::string field;
    std::vector<std::string> result;
    while (std::getline(stream, field, delimiter)) {
        result.push_back(field);
    }
    return result;
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user func
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void printList(const vector<int>& list) {
    // print
    for(int i = 0; i < list.size(); i++){
        printf("%d", list[i]);
        if(i != list.size() -1){
            printf(" ");
        }
    }
    printf("\n");
}



//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// logic
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Node;
using Container = std::map<int, std::shared_ptr<Node>>;
class Node{
private:
    Container *container;
    int id;
    int left;
    int right;
    int parentId;
public:
    Node(Container* container, int id, int left, int right)
        : container(container)
        , id(id)
        , left(left)
        , right(right)
        , parentId(-1)
    {
    }

    int Id(){
        return this->id;
    }
    int Left(){
        return this->left;
    }
    int Right(){
        return this->right;
    }

    void ParentId(int id){
        this->parentId = id;
    }

    int ParentId(){
        return this->parentId;
    }

    int Sibling(){
        // root
        if (this->parentId == -1) return -1;
        if (this->Parent()->Left() == this->id) {
            return this->Parent()->Right();
        } else {
            return this->Parent()->Left();
        }
    }

    int Degree(){
        int result = this->left == -1 ? 0 : 1;
        result   += this->right == -1 ? 0 : 1;
        return result;
    }

    int Depth(){
        if(this->parentId == -1){
            return 0;
        }else{
            return this->Parent()->Depth() + 1;
        }
    }

    int Height(){
        if(this->left + this->right == -2) return 0;
        int lHeight = this->left == -1 ? 0 : (*this->container)[this->left]->Height();
        int rHeight = this->right == -1 ? 0 : (*this->container)[this->right]->Height();
        return std::max(lHeight, rHeight) + 1;
    }

    string Type(){
        if(this->parentId == -1)
        {
            return "root";
        }
        if(this->left + this->right == -2)
        {
            return "leaf";
        }
        else
        {
            return "internal node";
        }
    }

    std::shared_ptr<Node> Parent(){
        if(this->parentId == -1) return nullptr;
        return (*this->container)[this->parentId];
    }
    bool HasLeft(){
        return this->left != -1;
    }
    bool HasRight(){
        return this->right != -1;
    }
    
};

void Preorder(const Container& container, int id){
    auto cur = container.at(id);
    printf(" %d", cur->Id());
    if (cur->HasLeft()) Preorder(container, cur->Left());
    if (cur->HasRight()) Preorder(container, cur->Right());
}


void Inorder(const Container& container, int id){
    auto cur = container.at(id);
    if(!cur->HasLeft() && !cur->HasRight()){
        printf(" %d", cur->Id());
        return;
    }
    if (cur->HasLeft()) Inorder(container, cur->Left());
    printf(" %d", cur->Id());
    if (cur->HasRight()) Inorder(container, cur->Right());
}

void StackPostorder(vector<int>& stack, const Container& container, int id){
    auto cur = container.at(id);
    if(!cur->HasLeft() && !cur->HasRight()){
        stack.push_back(cur->Id());
        return;
    }
    if (cur->HasLeft()){
        StackPostorder(stack, container, cur->Left());
    }
    if (cur->HasRight()) {
        StackPostorder(stack, container, cur->Right());
    }
    stack.push_back(cur->Id());
}

int FindRoot(const Container& container){
    for(auto each: container){
        if(each.second->ParentId() == -1){
            return each.first;
        }
    }
    return -1;
}

// return left , rite
// input parent ( and each list)
// sepalate left and right
// find highest node
// return

// 結局ツリーを作るしかないかな
// pre 範囲を決める？
// pre 範囲の中で一番先に出てくるのが親
// 再帰で子を受け取る
// リストを作る
// in 範囲の中で親を起点に分割
// 戻る？

// 面倒なので static
static Container container;

int FindFirstValue(const vector<int>& p, const vector<int>& s){
    for(int i = 0; i < p.size(); i++){
        for(auto needle: s){
            if(needle == p[i]) return needle;
        }
    }
    // printf("Error\n");
    return -1;
}

typedef struct _siblings{
    int Left;
    int Right;
} Siblings;

Siblings GetChild(const vector<int>& preorder, const vector<int>& inorder, int parent, int begin, int end){
    // printf("begin: %d, end: %d, parent: %d\n", begin, end, parent);
    if(end == begin) {
        return { -1, -1 };
    }

    // index in inorder
    int p = -1;
    for(int i = 0; i < inorder.size(); i++){
        if (parent == inorder[i]) {
            p = i;
            break;
        }
    }
    // left, right
    int leftId = -1;
    int rightId = -1;
    vector<int> left;
    for(int i = begin; i < p; i++){
        left.push_back(inorder[i]);
    }
    vector<int> right;
    for(int i = p + 1; i <= end; i++){
        right.push_back(inorder[i]);
    }
    leftId = FindFirstValue(preorder, left);
    rightId = FindFirstValue(preorder, right);
    // printf("left:%d right: %d\n", leftId, rightId);

    if (leftId != -1){
        auto children = GetChild(preorder, inorder, leftId, begin, p - 1);
        auto child = std::make_shared<Node>(&container, leftId, children.Left, children.Right);
        child->ParentId(parent);
        container.insert(std::make_pair(leftId, child));
    } 
    if (rightId != -1){
        auto children = GetChild(preorder, inorder, rightId, p + 1, end);
        auto child = std::make_shared<Node>(&container, rightId, children.Left, children.Right);
        child->ParentId(parent);
        container.insert(std::make_pair(rightId, child));
    }
    return {leftId, rightId};
}

void CreateTree(const vector<int>& preorder, const vector<int>& inorder){
    int rootId = preorder[0];
    auto siblings = GetChild(preorder, inorder, rootId, 0, preorder.size() - 1);
    auto root = std::make_shared<Node>(&container, rootId, siblings.Left, siblings.Right);
    container.insert(std::make_pair(rootId, root));
}

void solve() {
    // reconstruction of a tree
    int n;
    scanf("%d", &n);
    vector<int> preorder(n);
    vector<int> inorder(n);
    for(int i = 0; i < n; i++){
        int v;
        scanf("%d", &v);
        preorder[i] = v;
    }
    for(int i = 0; i < n; i++){
        int v;
        scanf("%d", &v);
        inorder[i] = v;
    }

    CreateTree(preorder, inorder);
    vector<int> stack;
    StackPostorder(stack, container, FindRoot(container));
    printList(stack);
    // for(auto each: container){
    //     printf("node %d: parent = %d, sibling = %d, degree = %d, depth = %d, %s [%d, %d]\n", 
    //         each.first,
    //         each.second->ParentId(),
    //         each.second->Sibling(),
    //         each.second->Degree(),
    //         each.second->Depth(),
    //         each.second->Type().c_str(),
    //         each.second->Left(), each.second->Right()
    //     );
    // }


    
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// main
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main() {
    solve();
    return 0;
}


Measured memory_bytes: 3284