Permutation Sort

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

One day (call it day 0), you find a permutation PP of NN integers written on the blackboard in a single row. Fortunately you have another permutation QQ of NN integers, so you decide to play with these permutations.

Every morning of the day 1, 2, 3 you rewrite every number on the blackboard in such a way that erases the number xx and write the number Q_xQ\_x at the same position. Please find the minimum non-negative integer dd such that in the evening of the day dd the sequence on the blackboard is sorted in increasing order.

입력

The input consists of a single test case in the format below.

The first line of the input contains an integer NN (1N2001 \le N \le 200). The second line contains NN integers P_1P\_1, \ldots, P_NP\_N (1P_iN1 \le P\_i \le N) which represent the permutation PP. The third line contains NN integers Q_1Q\_1,\ldots,Q_NQ\_N (1Q_iN1 \le Q\_i \le N which represent the permutation QQ.

출력

Print the minimum non-negative integer dd such that in the evening of the day dd the sequence on the blackboard is sorted in increasing order. If such dd does not exist, print 1-1 instead. It is guaranteed that the answer does not exceed 101810^{18}.