QOJ.ac

QOJ

Límite de tiempo: 2 s Límite de memoria: 1024 MB Puntuación total: 100 Hackeable ✓

#16890. Sequence Magic

Estadísticas

Hanbyeol planned a special event for the UCPC finals, which are being held offline for the first time in three years: sharing a raspberry pie with the participants! Hanbyeol divided the cylindrical pie into $M$ equal pieces, each shaped like a sector, and placed one raspberry on each piece. He then labeled the pieces from $1$ to $M$ in clockwise order.

Upon hearing that a total of $N$ ($N \le M$) participants would attend the competition, Hanbyeol pre-assigned a specific pie piece to each participant. However, just as Hanbyeol was about to distribute the pieces, one participant pointed to a raspberry on the pie and declared, "If you don't give me that raspberry, I'll solve all the problems in 10 minutes!" Soon, other participants began to state which raspberry they wanted, and eventually, every participant requested a specific raspberry.

To satisfy the participants' requests, Hanbyeol wants to adjust the positions of the raspberries on the pie. However, these raspberries are sensitive to environmental changes and will spoil quickly unless moved in the following manner:

  • Select one piece and move all raspberries on that piece to the very next piece.

Here, the piece immediately following piece $1$ is piece $2$, the piece immediately following piece $2$ is piece $3$, ..., the piece immediately following piece $M-1$ is piece $M$, and the piece immediately following piece $M$ is piece $1$.

Since spoiled raspberries negatively affect others, Hanbyeol wants to satisfy all participants' requests by moving the raspberries the minimum number of times without letting any of them spoil. Let's help Hanbyeol prevent the disaster of the competition ending in 10 minutes.

Note that participants do not mind if they end up eating other raspberries along with the one they wanted, and even if you select a piece with multiple raspberries, it counts as a single move.

Input

The first line contains the number of pie pieces $M$ and the number of participants $N$, separated by a space ($1 \le N \le M \le 300\,000$).

The second line contains $N$ integers $a_1, \dots, a_N$ separated by spaces ($1 \le a_i \le M$). $a_i$ represents the piece number assigned to the $i$-th participant, and all $a_i$ are distinct.

The third line contains $N$ integers $b_1, \dots, b_N$ separated by spaces ($1 \le b_i \le M$). $b_i$ represents the piece number where the raspberry desired by the $i$-th participant is currently located.

Output

If it is possible to satisfy all participants' requests without spoiling the raspberries, output the minimum number of moves required. Otherwise, output $-1$.

Examples

Input 1

5 2
3 5
1 4

Output 1

3

Input 2

3 2
3 2
1 2

Output 2

5

Input 3

4 3
1 3 4
1 1 3

Output 3

-1

Note

In the first example, all participants' requests can be satisfied by moving the raspberries a total of 3 times in the following way. There is no way to satisfy them with fewer moves.

i. Move all raspberries on piece $1$ to piece $2$. ii. Move all raspberries on piece $2$ to piece $3$. iii. Move all raspberries on piece $4$ to piece $5$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.