At Zeyu's school, a certain circle game has been getting all the craze lately.
The game consists of identical circular spinners, each containing equally-sized sectors labelled in order from to . A small arrow at the top of the spinner points to one of the sectors to represent the sector it is currently on.
In this game, two players race each other to set all spinners to point to the same number. They do so by picking a spinner and change the arrow to increase or decrease by of the current sector it is pointing at. Increasing by at the sector will move the arrow to the sector, and decreasing by at the sector will move the arrow to the sector. Doing this will cost them second every time. The winner will be the player who does this the fastest.
Zeyu wants to become the coolest kid in his grade, and aims to do so by becoming the very best at this game. Of course, he isn't very good at it and needs your aid. Help Zeyu find the minimum time to solve a given configuration of circles.
Input Specification
The first line will contain two integers and , the number of spinners and the number of sectors.
The second line will contain integers , with representing the current sector the spinner is pointing at.
Output Specification
In the first and only line of the output, your program should print an integer representing the minimum number of seconds required to set all spinners to the same number.
Constraints
Subtask 1 [5%]
Subtask 2 [15%]
Subtask 3 [80%]
No additional constraints.
Sample Input 1
3 8
2 3 8
Sample Output 1
3
Explanation for Sample 1
By setting all the spinners to , we can do it in seconds:
3 => 2
8 => 1 => 2
This is the fastest way.
Sample Input 2
5 10
2 4 3 9 1
Sample Output 2
7
Explanation for Sample 2
By setting all the spinners to , we can do it in seconds:
4 => 3 => 2
3 => 2
9 => 10 => 1 => 2
1 => 2
Comments