# 样例输入

10
0 1 9 3 8 4 2 7 5 6
3 9 8 1 2 4 0 5 7 6

3
5

# 算法分析

#include<iostream>

using namespace std;

long tree_depth(long *A ,long *B , long start , long end,  long A_pos,long &distance);//用来计算树的高度
int main(void){
long num = 0;
cin >> num;
if(num < 0){
cout << 0 <<endl;
cout << 0 << endl;
return 0;
}
long *A = new long[num];//用来存储前序序列
long *B = new long[num];//用来存储中序序列
for(long i = 0; i < num; ++i){
cin >> A[i];
}
for(long i = 0; i < num; ++i){
cin >> B[i];
}
long distance  = 0;
int tree_d = tree_depth(A, B, 0, num - 1, 0, distance);

cout << tree_d <<endl;
cout << distance  << endl;
delete []B;
B = NULL;
delete []A;
A = NULL;

}

long tree_depth(long * A, long * B, long start, long end, long A_pos, long & distance){
if (start > end){
distance = 0;
return -1;
}
if (start == end){
distance = 0;
return 0;
}
long root_num = A_pos;//用来存储根在中序序列位置
for(long j = start; j <= end; ++j){
if(B[j] == A[A_pos]){
root_num =j;
break;
}
}
long distance1 = distance, distance2 = distance;
long left_high = tree_depth(A, B, start, root_num - 1, A_pos+1, distance1) ;
long right_high = tree_depth(A, B, root_num + 1, end,  A_pos+1+(root_num - start), distance2);
distance = distance1 > distance2 ? distance1 : distance2;
if (right_high + left_high + 2> distance)
distance = left_high + right_high + 2;
return left_high > right_high?(left_high + 1): (right_high + 1);
}