atcoder#ARC077C. [ARC077E] guruguru

[ARC077E] guruguru

Score : 700700 points

Problem Statement

Snuke is buying a lamp. The light of the lamp can be adjusted to mm levels of brightness, represented by integers from 11 through mm, by the two buttons on the remote control.

The first button is a "forward" button. When this button is pressed, the brightness level is increased by 11, except when the brightness level is mm, in which case the brightness level becomes 11.

The second button is a "favorite" button. When this button is pressed, the brightness level becomes the favorite brightness level xx, which is set when the lamp is purchased.

Snuke is thinking of setting the favorite brightness level xx so that he can efficiently adjust the brightness. He is planning to change the brightness n1n-1 times. In the ii-th change, the brightness level is changed from aia_i to ai+1a_{i+1}. The initial brightness level is a1a_1. Find the number of times Snuke needs to press the buttons when xx is set to minimize this number.

Constraints

  • 2n,m1052 \leq n,m \leq 10^5
  • 1aim1 \leq a_i\leq m
  • aiai+1a_i \neq a_{i+1}
  • nn, mm and aia_i are integers.

Input

Input is given from Standard Input in the following format:

nn mm

a1a_1 a2a_2ana_n

Output

Print the minimum number of times Snuke needs to press the buttons.

4 6
1 5 1 4
5

When the favorite brightness level is set to 11, 22, 33, 44, 55 and 66, Snuke needs to press the buttons 88, 99, 77, 55, 66 and 99 times, respectively. Thus, Snuke should set the favorite brightness level to 44. In this case, the brightness is adjusted as follows:

  • In the first change, press the favorite button once, then press the forward button once.
  • In the second change, press the forward button twice.
  • In the third change, press the favorite button once.
10 10
10 9 8 7 6 5 4 3 2 1
45