Company: AT&T_6march
Difficulty: medium
Ship Locks A ship has n doors, each held by a lock. The crew can board only when every lock shows the same value, and locks[i] is the value the i -th lock starts on. Every lock is a dial whose face carries the values 1, 2, ..., k arranged in a ring. One operation turns a single dial one notch: if the dial shows 1 , it can be turned to 2 or to k ; if the dial shows k , it can be turned to k - 1 or to 1 ; otherwise it can be turned to one less or one more than its current value. So the values 1 and k are neighbours, exactly like any other consecutive pair. Find the minimum total number of operations needed to bring all n locks to a common value. Input Format First line: the integer k . Second line: the integer n , the number of locks. Third line: n integers locks[0] ... locks[n-1] . The values may be split across lines in any way; only their order and count matter. Output Format Print one integer — the minimum number of operations required. The answer can exceed the range of a 32-bit sig