#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

struct Node {
    struct Node* parent;
    struct Node* left;
    struct Node* right;
    int id;
};

static struct Node* CreateNode(int id) {
    struct Node* p_node = malloc(sizeof(struct Node));

    p_node->parent = p_node->left = p_node->right = NULL;
    p_node->id = id;

    return p_node;
}

static void DestroyNode(struct Node* p_node) {
    free(p_node);

    return;
}

static int FindRoot(const int pre[], int n, const int in[], int l, int r) {
    int i, j;

    for ( i = 0; i < n; i++ ) {
        for ( j = l; j < r; j++ ) {
            if ( pre[i] == in[j] ) {
                return pre[i];
            }
        }
    }
    
    return -1;
}

static int GetRootPos(int root, const int in[], int s, int e) {
    int i;
    
    for ( i = s; i < e; i++ ) {
        if ( in[i] == root ) {
            return i;
        }
    }
    assert( 0 );
    
    return -1;
}

static struct Node* Reconstruction(struct Node* p_parent, const int pre[], int n, const int in[], int l, int r) {
    int root;
    int in_index;
    struct Node* p_root;
    
    if ( l >= r ) {
        return NULL;
    }

    root = FindRoot(pre, n, in, l, r);
    in_index = GetRootPos(root, in, l, r);
    
    p_root = CreateNode(root);
    p_root->parent = p_parent;

    p_root->left = Reconstruction(p_root, pre, n, in, l, in_index);
    p_root->right = Reconstruction(p_root, pre, n, in, in_index + 1, r);

    return p_root;
}

static void PrintPostorder(const struct Node* p_root) {

    if ( p_root == NULL ){
        return;
    }

    PrintPostorder(p_root->left);
    PrintPostorder(p_root->right);
    printf("%d", p_root->id);

    if ( p_root->parent ) {
        printf(" ");
    }

    return;
}

static void DestroyTree(struct Node *p_root) {
    if ( !p_root ) {
        return;
    }

    DestroyTree(p_root->left);
    DestroyTree(p_root->right);
    DestroyNode(p_root);

    return;
}

int main(void) {
    int n;
    int i;
    int *pre, *in;
    struct Node* p_root;

    scanf("%d\n", &n);
    pre = malloc(sizeof(int) * n);
    in  = malloc(sizeof(int) * n);

    for ( i = 0; i < n; i++ ) {
        int d;
        scanf("%d", &d);
        pre[i] = d;
    }
    for ( i = 0; i < n; i++ ) {
        int d;
        scanf("%d", &d);
        in[i] = d;
    }

    p_root = Reconstruction(NULL, pre, n, in, 0, n);

    PrintPostorder(p_root);
    printf("\n");

    DestroyTree(p_root);
    free(pre);
    free(in);    

    return 0;
}
            
        

