You are given two permutations and of length .
In one operation, you can choose one of the permutations and perform a cyclic shift of it by one position to the left. That is, the permutation
is transformed into
You may perform operations on both permutations in any order.
You need to obtain such permutations that for every from to , the sum of the first elements of permutation is not less than the sum of the first elements of permutation .
In other words, for every the following must hold:
Find the minimum total number of operations needed.
The first line contains an integer (1 \leq n \leq 5000) — the length of the permutations.
The second line contains pairwise distinct integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq n) — permutation .
The third line contains pairwise distinct integers b_1, b_2, \ldots, b_n (1 \leq b_i \leq n) — permutation .
Print one integer — the minimum total number of operations needed so that for every from to , the sum of the first elements of permutation is not less than the sum of the first elements of permutation .
Example 1
Example 2
In the first example, the condition is already satisfied without any operations, so the answer is .
In the second example, you can cyclically shift permutation twice and permutation once:
After that, the prefix sums of permutation are 3, 7, 8, 10, and those of permutation are 2, 6, 7, 10. Thus, all required inequalities hold.
It is impossible to achieve this in fewer than operations, so the answer is .
Subtask ( points): n \le 8.
Subtask ( points): n \le 100.
Subtask ( points): n \le 1000.
Subtask ( points): No additional constraints.