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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

/*   ayushkmr                 **
**   Ayush Kumar			  **
**   IIT (ISM) Dhanbad        */
/*#pragma GCC optimize("O3")
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp>*/
#include <stdio.h>
#include<bits/stdc++.h>
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp(x,y) make_pair((x),(y))
#define sz size()
#define ff first
#define ss second
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define lp(i,n) for(i=0; i<(n); ++i)
#define lpp(i,z,n) for(i=z; i<=(n); ++i)
#define rlp(i,n) for(i=n-1; i>=0; --i)
#define lpit(it,a) for(__typeof(a.begin()) it = a.begin(); it != a.end(); ++it)
#define mset(m,v) memset(m,v,sizeof(m))
#define Abs(a,b) ((a) < (b) ? (b)-(a) : (a)-(b))
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define remax(a,b) (a)=max((a),(b));
#define remin(a,b) (a)=min((a),(b));
#define nu 100005
#define pnu 10
#define fnu 10
#define MOD 1000000007
#define MAX 1000000000
#define MAXL 1000000000000000000LL
#define nmod(a,b) ((a%b)+b)%b
#define LOG 11
#ifdef _WIN32
	#define pc putchar
	#define gc getchar
#else
	#define pc putchar_unlocked
	#define gc getchar_unlocked
#endif
 
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<ll> vl;
typedef stack<int> sti;
typedef queue<int> qi;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef unordered_map<int,int> ump;
typedef map<int,int> mpp;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

inline int in(){int N=0;register char c=gc();while( c < 48 || c > 57 ){c=gc();}while(c>47 && c< 58){N = (N << 3) + (N << 1) + (c - 48);c=gc();}return N;}
inline ll inl(){ll N=0;register char c=gc();while( c < 48 || c > 57 ){c=gc();}while(c>47 && c< 58){N = (N << 3) + (N << 1) + (c - 48);c=gc();}return N;}
inline int inp(){int N=0,sign=1;register char c=gc();while( c < 48 || c > 57 ){if(c=='-')sign=0; c=gc();}while(c>47 && c< 58){N = (N << 3) + (N << 1) + (c - 48);c=gc();}return (sign?N:(-N));}
inline ll inpl(){ll N=0,sign=1;register char c=gc();while( c < 48 || c > 57 ){if(c=='-')sign=0; c=gc();}while(c>47 && c< 58){N = (N << 3) + (N << 1) + (c - 48);c=gc();}return (sign?N:(-N));}
inline bool inb(){char c=gc();while( c < 48 || c > 57 ){c=gc();}return(c=='0'?0:1);}
inline ll ModP(ll b, ll e) {b %= MOD;ll r = 1;while (e>0) {if (e & 1) r = (r * b) % MOD;b = (b * b) % MOD;e >>= 1;}return r;}
bool pri[pnu]={1,1,0,0};
vi prm;
inline void prime_init() {int i,j;for(i=3;i*i<pnu;i+=2){if(!pri[i]){for(j=i*i;j<=pnu;j+=2*i){pri[j]=1;}}}}
inline void prime_asgn() {int i;prm.pb(2);for(i=3;i<pnu;i+=2)if(!pri[i])prm.pb(i);}
inline bool prime_check(ll i) { if(i==1) return 0; if(i<4) return 1; if(i&1) { if(i<pnu) {if(!pri[i]) return 1;else return 0; } else { int j,x; for(j=1;((x=prm[j])*x)<=i;j++) { if(!(i%x)) return 0; } return 1; } } else return 0; }
inline bool rngprm_check(int i) {if(i==1) return 0;if(i<4) return 1;if(i&1) {if(!pri[i]) return 1;else return 0;}else return 0;}
inline int npf(ll s){int j,x;int c; for(c=j=0;((x=prm[j])*prm[j])<=s;j++){ if(!(s%x)){ c++; while(!(s%x)) { s/=x; }}} if(s>1) c++; return(c); }
inline int nf(ll s) { if(s==1) return 1; int j,x,f=1; int c=1; for(j=0;((x=prm[j])*prm[j])<=s;c=1,j++) { if(!(s%x)){ while(!(s%x)){ s/=x; c++; } f=f*c; }} if(s>1) { f=f<<1;} return(f);}
vii nfa;
inline void nfx(ll s) {nfa.clear();if(s==1) return;int j,x,c=0;for(j=0;((x=prm[j])*prm[j])<=s;c=0,j++) { if(!(s%x)){while(!(s%x)){ s/=x; c++;} nfa.pb(mp(x,c)); }} if(s>1) {nfa.pb(mp(s,1));}}
ll fc[fnu];
inline ll InverseEuler(ll n){ return ModP(n,MOD-2); }
inline ll nCr(int n, int r){ if(r>n) return 0; return (fc[n]*((InverseEuler(fc[r]) * InverseEuler(fc[n-r])) % MOD)) % MOD;}
inline void facs(){ fc[0]=fc[1]=1; for(ll i=2;i<fnu;i++){ fc[i]=(i*fc[i-1])%MOD; } }

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int main()
{
	//ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);freopen ("in.txt","r",stdin); freopen ("out.txt","w",stdout);
    ll n=in(),m=in(),bg,en,m1,m2,v1,v2,x,y,i,j,a[nu],sm,d,xx,yy,a1,a2,ans,d2;
    lp(i,n)
    {
        a[i]=in();
    }
    bg=1;
    en=m+m+m;
    while(en-bg>50)
            {
            m1=bg+(en-bg)/3;
            m2=en-(en-bg)/3;
        d=m1;
        if(d>m)
        {
            d-=m;
        }
        if(d>m)
        {
            d-=m;
        }
        d2=m2;
        if(d2>m)
        {
            d2-=m;
        }
        if(d2>m)
        {
            d2-=m;
        }
            //d=m1;
            v1=v2=0;
            for(i=0;i<n-1;i++)
                {
                x=a[i];
                y=a[i+1];
                if(y<x)
                {
                    xx=y+m-x;
                }
                else
                {
                    xx=y-x;
                }
                a1=xx;
                a2=xx;
                x=d;
                if(y<x)
                {
                    xx=y+m-x;
                }
                else
                {
                    xx=y-x;
                }
                yy=1+xx;
                remin(a1,yy);
                v1+=a1;
                x=d2;
                if(y<x)
                {
                    xx=y+m-x;
                }
                else
                {
                    xx=y-x;
                }
                yy=1+xx;
                remin(a2,yy);
                v2+=a2;
            }
        //cout<<m1<<" "<<v1<<" "<<m2<<" "<<v2<<"\n";
            if(v1<v2)
                {
                en=m2;
            }
            else if(v1>v2)
                {
                bg=m1;
            }
            else
                {
                bg=m1;
                en=m2;
            }
        }
        ans=MAXL;
        for(j=bg;j<=en;j++)
            {
            v1=0;
            for(i=0;i<n-1;i++)
                {
                x=a[i];
                y=a[i+1];
                if(y<x)
                {
                    xx=y+m-x;
                }
                else
                {
                    xx=y-x;
                }
                a1=xx;                
                x=j;
                if(x>m)
                {
                    x-=m;
                }
                if(x>m)
                {
                    x-=m;
                }
                if(y<x)
                {
                    xx=y+m-x;
                }
                else
                {
                    xx=y-x;
                }
                yy=1+xx;
                remin(a1,yy);
                v1+=a1;                
            }
            remin(ans,v1);
        }
    cout<<ans;
	return 0;
}