#include <bits/stdc++.h>
using namespace std;
const int MAXN=40;
int n, m;
int mid[MAXN],in[MAXN];
struct node
{
int key;
struct node *l,*r;
node()
{
l=r=NULL;
}
};
struct node *build(struct node *root,int *mid,int *in,int l)
{
if(l==0)
return 0;
root =new node();
root->key=*in;
int pos=0;
for(;pos<l;++pos)
if(mid[pos]==*in)
break;
root->l=build(root->l,mid,in+1,pos);
root->r=build(root->r,mid+pos+1,in+pos+1,l-pos-1);
return root;
};
void get_ans(struct node *root)
{
if(!root)
return;
int n = 0;
queue<node*>q;
q.push(root);
while(!q.empty())
{
node *k=q.front();
q.pop();
n++;
if(n == 1)
printf("%d",k->key);
else
printf(" %d",k->key);
if(k->r)q.push(k->r);
if(k->l)q.push(k->l);
}
printf("\n");
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;++i)
scanf("%d",&mid[i]);
for(int i=0;i<n;++i)
scanf("%d",&in[i]);
struct node *root=NULL;
root=build(root,mid,in,n);
get_ans(root);
return 0;
}
using namespace std;
const int MAXN=40;
int n, m;
int mid[MAXN],in[MAXN];
struct node
{
int key;
struct node *l,*r;
node()
{
l=r=NULL;
}
};
struct node *build(struct node *root,int *mid,int *in,int l)
{
if(l==0)
return 0;
root =new node();
root->key=*in;
int pos=0;
for(;pos<l;++pos)
if(mid[pos]==*in)
break;
root->l=build(root->l,mid,in+1,pos);
root->r=build(root->r,mid+pos+1,in+pos+1,l-pos-1);
return root;
};
void get_ans(struct node *root)
{
if(!root)
return;
int n = 0;
queue<node*>q;
q.push(root);
while(!q.empty())
{
node *k=q.front();
q.pop();
n++;
if(n == 1)
printf("%d",k->key);
else
printf(" %d",k->key);
if(k->r)q.push(k->r);
if(k->l)q.push(k->l);
}
printf("\n");
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;++i)
scanf("%d",&mid[i]);
for(int i=0;i<n;++i)
scanf("%d",&in[i]);
struct node *root=NULL;
root=build(root,mid,in,n);
get_ans(root);
return 0;
}