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)

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
const int INF=1e9,MOD=1e9+7,ohara=1e6+10;
const ll LINF=1e18;
using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define rrep(i,a,b) for(int i=(a);i<(b);i++)
#define rrrep(i,a,b) for(int i=(a);i>=(b);i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout<<(x)<<endl
#define doublecout(a) cout<<fixed<<setprecision(15)<<a<<endl;
#define Cerr(x) cerr<<(x)<<endl
#define fi first
#define se second
#define P pair<ll,ll> 
#define m_p make_pair
#define V vector<ll> 
#define U_MAP unordered_map<ll,ll>

ll n,cnt,ans,a[ohara],b,c,d,tmp,tmpp,m,h,w,x,y,sum,pos,k,imo[ohara];
ld doua;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
//int dy[]={-1,0,1,-1,1,-1,0,1};
//int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"),s;
bool fl;
struct edge{int to,cost;};
ll kazu[ohara];
ll mae[ohara];
ll usiro[ohara];

//-------------------------↓↓↓↓↓↓------------------------

//------ 自分を天才だと信じる ------

int main(void){
        cin.tie(0);
        cout.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>n>>m;
    rep(i,n)cin>>a[i];
    rep(i,n-1){
            if(a[i]<=a[i+1]){
                    ans+=a[i+1]-a[i];
            }
            else{
                    ans+=a[i+1];
            }
    }
    //pa1 両端含み　x,x+1がa[i]+1からa[i+1]-1の中
    rep(i,n-1){
            if(a[i]+1<=a[i+1]-1){
                    imo[a[i]+1]++;
                    imo[a[i+1]]--;
            }
            else if(a[i+1]+1<=a[i]-1){
                imo[a[i]+1]++;
                imo[1]++;
                imo[a[i+1]]--;
            }
    }
    rrep(i,1,m+2)imo[i]+=imo[i-1];
    
    //pa2
    rrep(i,1,n){
        mae[a[i]]+=max(max((a[i]-a[i-1]+m),0LL)%m-1,0LL);
        if(a[i]<a[i-1]){
            usiro[a[i-1]]=mae[a[i]];
        }
    }
    
    cnt=ans;
    rrep(i,2,m+1){
        //pa1
        cnt-=max(imo[i-1],0LL);
        cnt+=max(mae[i-1],0LL);
        cnt-=(usiro[i],0LL);
        ans=min(ans,cnt);
        //Cout(cnt);
        //cout<<imo[i-1]<<" "<<mae[i-1]<<"\n";
    }
    Cout(ans);
    return 0;
}