100 #ABC160C. [ABC160C] Traveling Salesman around Lake

[ABC160C] Traveling Salesman around Lake

Score : 300300 points

Problem Statement

There is a circular pond with a perimeter of KK meters, and NN houses around them.

The ii-th house is built at a distance of AiA_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the NN houses.

Constraints

  • 2K1062 \leq K \leq 10^6
  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 0A1<...<AN<K0 \leq A_1 < ... < A_N < K
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

KK NN

A1A_1 A2A_2 ...... ANA_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the NN houses.

20 3
5 10 15
10

If you start at the 11-st house and go to the 22-nd and 33-rd houses in this order, the total distance traveled will be 1010.

20 3
0 5 15
10

If you start at the 22-nd house and go to the 11-st and 33-rd houses in this order, the total distance traveled will be 1010.