Back To School '19: A Circular Game

View as PDF

Points: 10 (partial)
Time limit: 1.0s
Memory limit: 64M

Author:
Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

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 time required to set all spinners to the same number.

No further constraints.

3 8
2 3 8

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.

5 10
2 4 3 9 1

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