Problem:
Score : 700 points

Problem Statement

Snuke is buying a lamp.
The light of the lamp can be adjusted to m levels of brightness, represented by integers from 1 through m, 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 1, except when the brightness level is m, in which case the brightness level becomes 1.

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

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

Constraints

2 \leq n,m \leq 10^5

1 \leq a_i\leq m

a_i \neq a_{i+1}

n, m and a_i are integers.

Input

Input is given from Standard Input in the following format:

n m
a_1 a_2 … a_n

Output

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

Sample Input 1

4 6
1 5 1 4

Sample Output 1

5

When the favorite brightness level is set to 1, 2, 3, 4, 5 and 6, Snuke needs to press the buttons 8, 9, 7, 5, 6 and 9 times, respectively.
Thus, Snuke should set the favorite brightness level to 4.
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.

Sample Input 2

10 10
10 9 8 7 6 5 4 3 2 1

Sample Output 2

45

Solution in C (GCC 5.4.1) (judge verdict: Wrong Answer)

//#include "oj/atcoder.h"
//#include "std.h"
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <float.h>

#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define SWAP(T,a,b) do { T _tmp=a; a=b; b=_tmp; } while (0)

//#include "oj/io.h"

void print_ai(int *a, int n) {
	for (int i=0; i<n; i++) {
		if (i) printf(" ");
		printf("%d", a[i]);
	}
	printf("\n");
}

int scan_ai(int *a, int n) {
	for (int i=0; i<n; i++) {
		if (scanf("%d", &a[i]) != 1)
			return i;
	}
	return n;
}


//#include "oj/debug.h"

void print_hr() {
	printf("---------------------------\n");
}

void print_al(long *a, int n) {
	for (int i=0; i<n; i++) {
		if (i) printf(" ");
		printf("%ld", a[i]);
	}
	printf("\n");
}


#define T long
//#include "misc/array/partial_sum.h"

void array_partial_sum_revert(T *a, int n) {
	for (int i=n-1; i>0; i--)
		a[i] -= a[i-1];
}

//add val to a[s,t)
void array_partial_sum_range_add(T *a, int s, int t, T val) {
	a[s] += val;
	a[t] -= val;
}

void array_partial_sum_create(T *a, int n) {
	for (int i=0; i<n-1; i++)
		a[i+1] += a[i];
}


int main() {
	int n, m;
	while (scanf("%d %d", &n, &m)==2) {
		int a[n];
		scan_ai(a, n);
		/*
		steps from s to t:
		s<=t:
			x s t: t-s      0<x<=s
			s x t: t-x+1    s<x<=t
			s t x: t-s      t<x<=m
		s>t:
			x t s: t-x+1    0<x<=t
			t x s: t+m-s    t<x<=s
			t s x: t+m-x+1  s<x<=m
		
		sum[i=0..n-1](x) = g*x + h
			s = a[i]
			t = a[i+1]
		*/

		long g[m], h[m];
		memset(g, 0, sizeof(g));
		memset(h, 0, sizeof(g));
		array_partial_sum_revert(g, m);
		array_partial_sum_revert(h, m);

		for (int i=0; i<n-1; i++) {
			long s = a[i], t = a[i+1];
			if (s <= t) {
				//0<x<=s
				array_partial_sum_range_add(h, 0, s, t-s);
				//s<x<=t
				array_partial_sum_range_add(h, s, t, t+1);
				array_partial_sum_range_add(g, s, t, -1);
				//t<x<=m
				array_partial_sum_range_add(h, t, m, t-s);
			}
			else {
				//0<x<=t
				array_partial_sum_range_add(h, 0, t, t+1);
				array_partial_sum_range_add(g, 0, t, -1);
				//t<x<=s
				array_partial_sum_range_add(h, t, s, t+m-s);
				//s<x<=m
				array_partial_sum_range_add(h, s, m, t+m+1);
				array_partial_sum_range_add(g, s, m, -1);
			}
		}
		array_partial_sum_create(g, m);
		array_partial_sum_create(h, m);
		//print_al(g, m); print_al(h, m);

		long minz = INT_MAX;
		int minx = 0;
		for (int x=1; x<=m; x++) {
			long z = g[x-1] * x + h[x-1];
			//printf("g=%ld h=%ld z=%ld\n", g[x-1], h[x-1], z);
			if (z < minz) {
				minz = z;
				minx = x;
			}
		}
		//printf("%d\n", minx);
		printf("%ld\n", minz);
	}
}