#include<bits/stdc++.h>
using namespace std;
const int maxn=45;
int in[maxn],post[maxn],Hash[maxn],n;//题目数据范围N<=30, Hash数组不能设成35!!!
struct node{
int val;
node* left;
node* right;
node(int x):val(x),left(NULL),right(NULL){}
};
node* dfs(int pl,int pr,int il,int ir){//node*
if(pl>pr)return NULL;//
node* root= new node(post[pr]);
//node *root = new node; root->val=post[pr];
int k=Hash[root->val];
root->left=dfs(pl,pl+k-il-1,il,k-1);
root->right=dfs(pl+k-il,pr-1,k+1,ir);
return root;
}
void bfs(node* root){
int num=0;
queue<node*>q;
q.push(root);
while(!q.empty()){
node* nd=q.front();
q.pop();
printf("%d",nd->val);
++num;
if(num<n)printf(" ");
if(nd->left!=NULL) q.push(nd->left);
if(nd->right!=NULL) q.push(nd->right);
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;++i){
scanf("%d",&post[i]);
}
for(int i=0;i<n;++i){
scanf("%d",&in[i]);
Hash[in[i]]=i;
}
node* root=dfs(0,n-1,0,n-1);
bfs(root);
}