#abc150c. [abc150_c]Count Order

[abc150_c]Count Order

Problem Statement

We have two permutations PP and QQ of size NN (that is, PP and QQ are both rearrangements of (1, 2, ..., N)(1,~2,~...,~N)).

There are N!N! possible permutations of size NN. Among them, let PP and QQ be the aa-th and bb-th lexicographically smallest permutations, respectively. Find ab|a - b|.

Notes

For two sequences XX and YY, XX is said to be lexicographically smaller than YY if and only if there exists an integer kk such that Xi=Yi (1leqi<k)X_i = Y_i~(1 \\leq i < k) and Xk<YkX_k < Y_k.

Constraints

  • 2leqNleq82 \\leq N \\leq 8
  • PP and QQ are permutations of size NN.

Input

Input is given from Standard Input in the following format:

NN P1P_1 P2P_2 ...... PNP_N Q1Q_1 Q2Q_2 ...... QNQ_N

Output

Print ab|a - b|.


Sample Input 1

3
1 3 2
3 1 2

Sample Output 1

3

There are 66 permutations of size 33: (1, 2, 3)(1,~2,~3), (1, 3, 2)(1,~3,~2), (2, 1, 3)(2,~1,~3), (2, 3, 1)(2,~3,~1), (3, 1, 2)(3,~1,~2), and (3, 2, 1)(3,~2,~1). Among them, (1, 3, 2)(1,~3,~2) and (3, 1, 2)(3,~1,~2) come 22-nd and 55-th in lexicographical order, so the answer is 25=3|2 - 5| = 3.


Sample Input 2

8
7 3 5 4 2 1 6 8
3 8 2 5 4 6 7 1

Sample Output 2

17517

Sample Input 3

3
1 2 3
1 2 3

Sample Output 3

0