#include <stdio.h>
#define MAX 40

struct node
{
	int p;
	int l, r;
} typedef node;

int CreateTree(int, int, int, int);
int Position(int, int, int[], int);
void Postorder(int, int);

int P[MAX];
int I[MAX];
node N[MAX];

int main()
{
	int i;
	int n;
	int root;
	
	scanf("%d", &n);
	for(i = 0; i < n; i++)
	{
		scanf("%d", &P[i]);
	}
	for(i = 0; i < n; i++)
	{
		scanf("%d", &I[i]);
	}
	
	root = CreateTree(-1, n, 0, 0);
	
	Postorder(root, n);
}

int CreateTree(int parent, int n, int head_P, int head_I)
{
	int root;
	int root_P, root_I;
	int n_l, n_r;
	
	root = P[head_P];
	
	if(n == 0) return -1;

	root_P = head_P;
	root_I = Position(n, head_I, I, root);
	
	n_l = root_I - head_I;
	n_r = n - n_l - 1;
	
	N[root].p = parent;
	N[root].l = CreateTree(root, n_l, root_P + 1, head_I);
	N[root].r = CreateTree(root, n_r, root_P + 1 + n_l, root_I + 1);
	
	return root;
}

int Position(int n, int h, int A[], int s)
{
	int i;
	
	for(i = h; i < h + n; i++)
	{
		if(A[i] == s) return i;
	}
	return -1;
}

void Postorder(int id, int n)
{
	static int count = 0;
	
	if(id == -1) return;
	
	Postorder(N[id].l, n);
	Postorder(N[id].r, n);
	
	count++;
	if(count != n) printf("%d ", id);
	else printf("%d\n", id);
}