At Zeyu's school, a certain circle game has been getting all the craze lately.
The game consists of
In this game, two players race each other to set all
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
The second line will contain
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
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
4 => 3 => 2
3 => 2
9 => 10 => 1 => 2
1 => 2
Comments