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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using static System.Math;
using System.Numerics;
static class Program{
	const int mod=(int)1e9+7;
	const double eps=1e-11;
	static void Main(){
		Sc sc=new Sc();
		var s=sc.Ia;
		var a=sc.Ia;
		var imos=new Imos(s[1]+1,2);
		long p=0,g=0;
		for(int i = 1;i<s[0];i++) {
			if(a[i]>a[i-1]){
				imos.Add1(a[i-1]+1,a[i],0,1);
				p+=a[i]-a[i-1];
			}
			else{
				imos.Add1(a[i-1]+1,s[1],0,1);
				imos.Add1(1,a[i],s[1]-a[i-1],1);
				p+=s[1]+a[i]-a[i-1];
			}
		}
		imos.Go();
		for(int i = 1;i<=s[1];i++) {g=Max(g,imos[i]);}
		Console.WriteLine("{0}",p-g);
	}
}
public class Imos{
	private const int mod=(int)1e9+7;
	private long[] a,h,c,ra;
	private int o,n;
	public Imos(int n,int o){
		this.o=o;
		this.n=n;
		a=new long[n+o];
		h=new long[n+o];
		c=new long[o];
		ra=new long[n+o];
		ra[0]=ra[1]=1;
		for(int i = 2;i<ra.Length;i++) {
			ra[i]=ra[i-1]*i;
			if(i>o){ra[i]/=i-o+1;}
		}
		for(int i = 0;i<((o+1)>>1);i++) {ra[o-i-1]=ra[i]*=ra[o-i-1];}
	}
	public void Add1(int l,int r,long a,long b){
		h[l]+=a;
		h[l+1]-=a-b;
		h[r+1]-=a+(r-l+1)*b;
		h[r+2]+=a+(r-l)*b;
	}
	public void Add2(int l,int r,long b){
		h[l]+=b;
		h[l+1]+=b;
		h[r+1]-=(r-l+2)*(r-l+2)*b;
		h[r+2]+=((r-l+1)*(r-l+1)*2+(r-l+1)*2-1)*b;
		h[r+3]-=(r-l+1)*(r-l+1)*b;
	}
	public void Addc(int l,int r,long b){
		int p=r-l;
		h[l]+=b;
		long f=ra[p+o]*b;
		for(int i = 1;i<=o;i++) {
			h[r+i]+=f/ra[i-1]*((i&1)==1?-1:1);
			f*=p+i;
			f/=p+i+1;
		}
	}
	public void Addt(int l,int r,long b){
		int p=r-l;
		h[l]+=b;
		h[r+1]-=(p+2)*(p+3)/2*b;
		h[r+2]+=(p+1)*(p+3)*b;
		h[r+3]-=(p+1)*(p+2)/2*b;
	}
	public void Go(){
		for(int i = 1;i<n;i++) {
			c[0]+=h[i];
			for(int j = 1;j<o;j++) {c[j]+=c[j-1];}
			a[i]=c[o-1];
		}
	}
	public long this[int i]{get{return a[i];}}
	public override string ToString()=>string.Join(" ",a);
}
public class Sc{
	public int I{get{return int.Parse(Console.ReadLine());}}
	public long L{get{return long.Parse(Console.ReadLine());}}
	public double D{get{return double.Parse(Console.ReadLine());}}
	public string S{get{return Console.ReadLine();}}
	public int[] Ia{get{return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
	public object[] Oa{get{return Console.ReadLine().Split();}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public int[] Ia3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),int.Parse);}
	public int[] Ia3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),int.Parse);}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),long.Parse);}}
	public long[] La3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),long.Parse);}
	public long[] La3(int a){return Array.ConvertAll((Console.ReadLine()+" "+a.ToString()).Split(),long.Parse);}
	public double[] Da2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),double.Parse);}}
	public double[] Da3(string a,string b){return Array.ConvertAll((a+Console.ReadLine()+b).Split(),double.Parse);}
	public T[] Arr<T>(int n,Func<T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f();}return a;}
	public T[] Arr<T>(int n,Func<int,T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i);}return a;}
	public T[] Arr<T>(int n,Func<string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(Console.ReadLine().Split());}return a;}
	public T[] Arr<T>(int n,Func<int,string[],T> f){var a=new T[n];for(int i=0;i<n;i++){a[i]=f(i,Console.ReadLine().Split());}return a;}
}