Cyclic Shifts#13060

Cyclic Shifts

1 second
256 megabytes
Very hard

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

p_1, p_2, \ldots, p_n

is transformed into

p_2, p_3, \ldots, p_n, p_1.

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:

a_1 + a_2 + \ldots + a_k \geq b_1 + b_2 + \ldots + b_k.

Find the minimum total number of operations needed.

Input

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 .

Output

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 .

Examples

Example 1

Loading
Loading

Example 2

Loading
Loading

Note

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:

[1, 2, 3, 4] \rightarrow [3, 4, 1, 2],
[3, 2, 4, 1] \rightarrow [2, 4, 1, 3].

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 .

Scoring

  1. Subtask ( points): n \le 8.

  2. Subtask ( points): n \le 100.

  3. Subtask ( points): n \le 1000.

  4. Subtask ( points): No additional constraints.