#ABC150C. [ABC150C] Count Order

[ABC150C] Count Order

Score : 300300 points

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, \sim 2, \sim ..., \sim 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(1i<k)X_i = Y_i \sim (1 \leq i < k) and Xk<YkX_k < Y_k.

Constraints

  • 2N82 \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|.

3
1 3 2
3 1 2
3

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

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