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)

/*
 * DATE : 2017-07-01
 * Algo :
*/
#include<bits/stdc++.h>
using namespace std;
/*------- Constants---- */

#define Long                    long long
#define Ulong                   unsigned long long
#define FOR(I,A,B)              for(long long i=0; i < n ; i++ )
#define REP(i,n)                for(long long i=0; i < n ; i++ )
#define mp                      make_pair
#define pb                      push_back
#define all(x)                  (x).begin(),(x).end()
#define PI                      acos(-1.0)
#define EPS                     1e-9
#define F                       first
#define S                       second
#define di(x)                   int x; input(x)
#define in(x)                   input(x)
#define in2(x,y)                in(x),in(y)
#define in3(x,y,z)              in(x),in2(y,z)
#define lc                      ((n)<<1)
#define rc                      ((n)<<1|1)
#define db(x)                   cout << #x << " -> " << x << endl;
#define ms(ara_name,value)      memset(ara_name,value,sizeof(ara_name))
#define IO                      ios_base::sync_with_stdio(0);cin.tie(0)
#define READ                    freopen("in.txt","r",stdin)
#define WRITE                   freopen("out.txt","w",stdout)
template<class T> inline void input(T &x) {
    register char c = getchar();x = 0;
    int neg = 0;
    for(; ((c<48 || c>57) && c != '-'); c = getchar());
    if(c=='-'){neg = 1;c = getchar();}
    for(; c>47 && c<58 ; c = getchar()){x = (x<<1) + (x<<3) + c - 48;}
    if(neg) x = -x;
}
inline long long bigmod(long long p,long long e,long long M){
    long long ret = 1;
    for(; e > 0; e >>= 1){
        if(e & 1) ret = (ret * p) % M;
        p = (p * p) % M;
    } return ret;
}
template <class T> inline T gcd(T a,T b){if(b==0)return a;return gcd(b,a%b);}
template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);}

/***************************** END OF TEMPLATE *******************************/
const int N = 3e5+5;
int n,m;
int a[N];
int f(int x,int y)
{
    int ret = 0;
    while(x!=y){
        x++;
        if(x>m) x=1;
        ret++;
    }
    return ret;
}
long long func(int pivot, int now, int nxt)
{
    int a = f(now,nxt) , b = 1 + f(pivot, nxt);
    if(a>b) {
        return b;
    }
    return a;
}
long long brute(int pivot)
{
    int ans = 0;
    for(int i = 1;i < n;i ++ ) {
        ans += min( (a[i+1] - a[i] + m) % m , 1 + (a[i+1] - pivot + m) % m);
    }
    return ans;
}
long long lazy[5*N], T[5*N], P[5*N], Ans[N];
void upd(int n,int b,int e,int i,int j,double k,double s)
{

    if(lazy[n]) {
        if(b!=e) {
            lazy[lc] = 1;
            lazy[rc] = 1;
            T[lc] += T[n];
            T[rc] += T[n];
            P[lc] += P[n];
            P[rc] += P[n];

            lazy[n] = 0;
            T[n] = 0;
            P[n] = 0;
        }

    }
    if(b>j||e<i)return;
    if(b>=i&&e<=j){
        T[n] += k;
        P[n] += s;
        lazy[n] = 1;
        return;
    }
    int mid = (b+e)/2;
    upd(lc,b,mid,i,j,k,s);
    upd(rc,mid+1,e,i,j,k,s);


}


void Trv(int n,int b,int e)
{
    if(lazy[n]) {
        if(b!=e) {
            lazy[lc] = 1;
            lazy[rc] = 1;
            T[lc] += T[n];
            T[rc] += T[n];
            P[lc] += P[n];
            P[rc] += P[n];

            lazy[n] = 0;
            T[n] = 0;
            P[n] = 0;
        }

    }
    if(b==e) {
        Ans[b] = P[n] + b * T[n];
        return;
    }
    int mid = (b+e)/2;
    Trv(lc,b,mid);
    Trv(rc,mid+1,e);
}
int main()
{

    cin>>n>>m;
    for(int i=1;i<=n;i++) cin >> a[i];

    for(int i = 1;i < n;i ++ ) {
        int nxt = a[i+1], now = a[i];
        if(nxt > now ) {
            upd(1,1,m,now+1,nxt,-1,nxt + 1);
            upd(1,1,m,nxt+1,m,0,nxt - now);
            upd(1,1,m,1,now,0,nxt- now);
        }
        else {
            int p = (nxt - now + m) % m;
            upd(1,1,m,now + 1, m, -1 , m + nxt + 1);
            upd(1,1,m,1,nxt,-1,nxt + 1);
            upd(1,1,m,nxt+1,now,0,p);
        }
    }
    Trv(1,1,n);
    long long ans  = 1e18;
    for(int i = 1;i <= m;i ++ ) {
        ans = min(ans , Ans[i]);
    }
    cout << ans << endl;

    return 0;
}