P26775 [AtCoder] Kendo
省选
题目描述

Problem Description

Kyo-to City is流行ing a sport called Kendo.

Kendo is a sport where two teams, each consisting of $ N $ people, compete against each other. Each participant in Kendo has an integer value called "Waza-me".

Kendo is conducted as follows:

  • First, each team decides the order and lines up in a single row.
  • Then, $ N $ Ikki-uchi (simultaneous attacks) are conducted in sequence.
  • The $ i $ ( $1 \leq i \leq N$ )-th Ikki-uchi is conducted between the $ i $-th person in each team.
  • In an Ikki-uchi, the person with the smaller Waza-me is defeated. If the Waza-me values are the same, it results in a draw, and neither is defeated.

Takahashi's team is about to compete against Aoki's team in Kendo. Takahashi's team's order has already been decided, but through a last-minute spy activity, Aoki's team's order is known to be arranged in ascending order of Waza-me, and their Waza-me values have been obtained. Therefore, Takahashi wants to rearrange his team's order so that none of his team members are defeated in any Ikki-uchi.

Due to various circumstances, the team order can only be changed by swapping two adjacent members. Since there is little time before the match, Takahashi wants to rearrange his team with the minimum number of swaps to ensure that none of his team members are defeated. You are asked to calculate and output the minimum number of swaps required. If it is impossible to rearrange the team to avoid any defeats, output $ -1 $.

Input Format

The input is given from the standard input in the following format:

$ N $ $ A_1 $ $ A_2 $ ... $ A_N $ $ B_1 $ $ B_2 $ ... $ B_N $

  • The first line contains an integer $ N $ ( $1 \leq N \leq 10^5$ ).
  • The second line contains $ N $ integers $ A_i $ ( $1 \leq i \leq N$ ), separated by spaces. $ A_i $ ( $0 \leq A_i \leq 10^9$ ) represents the Waza-me of the $ i $-th person in Takahashi's team.
  • The third line contains $ N $ integers $ B_i $ ( $1 \leq i \leq N$ ), separated by spaces. $ B_i $ ( $0 \leq B_i \leq 10^9$ ) represents the Waza-me of the $ i $-th person in Aoki's team.
  • $B_i \leq B_{i+1}$ ( $1 \leq i \leq N-1$ ) holds.

Output Format

Output the minimum number of swaps required to satisfy the conditions in one line. If it is impossible to satisfy the conditions no matter how the team is rearranged, output $ -1 $.

Sample Input and Output

Sample Input #1

3
6 2 4
1 3 5

Sample Output #1

2

Sample Input #2

3
7 2 6
1 3 5

Sample Output #2

1

Sample Input #3

3
8 8 8
2 7 9

Sample Output #3

-1

Notes/Hints

Partial Points

If all test cases satisfying the following constraints are correctly solved, $ 3 $ partial points will be awarded:

  • $N \leq 10^3$
输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 6 2 4 1 3 5
样例输出
2