#include<stdio.h>
#include<stdlib.h>
#define NIL -1

typedef struct{
  int l;
  int r;
  short put;
} Node;

Node *T;
short *P,*I;
int n;

void Postorder(int u,short a){  
  if(T[u].l!=NIL) Postorder(T[u].l,0);
  if(T[u].r!=NIL) Postorder(T[u].r,0);
  
  printf("%d",u);
  if(a!=NIL)printf(" ");
}

void reverse(int root,int p){
  int i,j,u; //i=P,j=I

  //printf("root=%d,p=%d,n=%d || ",root,p,n);
  //for(i=0;i<n;i++) printf("P[i]=%d,P[i].put=%hd ",P[i],T[P[i]].put);
  
    for(i=root;i<n;i++){
      T[ P[i] ].put=1;
      if(P[i]==I[p]) break;
      T[ P[i] ].l=P[i+1];
    }

    //printf(" i=%d\n",i);

  if(i!=n-1){
    for(j=p ;j<n; j++){
    
      if(T[ I[j] ].put == NIL){
	T[ I[j-1] ].r = P[i+1];
	reverse(i+1,j);
	return;	
      }
      
    }
  }
  
}
    

int main(){
  int i,j,a=0,u;  

  scanf("%d",&n);

  P=(short *)malloc(sizeof(short) * n);
  I=(short *)malloc(sizeof(short) * n);

  for(i=0;i<n;i++) scanf("%hd",&P[i]);
  for(i=0;i<n;i++){
    scanf("%hd",&I[i]);
    if(I[i]>a) a=I[i];
  }

  T=(Node *)malloc(sizeof(Node) * a);
  for(i=0;i<n;i++) T[ P[i] ].l = T[ P[i] ].r = T[ P[i] ].put = NIL;

  reverse(0,0);

  //for(i=0;i<n;i++) printf("i=%d,P[i]=%hd,T[ P[i] ].l=%d,T[ P[i] ].r=%d\n",i,P[i],T[P[i]].l,T[P[i]].r);

  Postorder(P[0],NIL);  
  printf("\n"); 

  return 0;
}

