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;}
}

Measured memory_bytes: 37396