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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
    
     
     
using namespace std;
template<typename T1, typename T2>inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; }
/** Interface */
     
inline int readChar();
template <class T = int> inline T readInt(); 
template <class T> inline void writeInt( T x, char end = 0 );
inline void writeChar( int x ); 
inline void writeWord( const char *s );
     
/** Read */
     
static const int buf_size = 4096;
     
inline int getChar() {
    static char buf[buf_size];
    static int len = 0, pos = 0;
    if (pos == len) {
        pos = 0, len = fread(buf, 1, buf_size, stdin);
    }
    if (pos == len) {
        return -1;
    }
    return buf[pos++];
}
     
inline int readChar() {
    int c = getChar();
    while (c <= 32) {
        c = getChar();
    }
    return c;
}
     
template <class T>
inline T readInt() {
    int s = 1, c = readChar();
    T x = 0;
    if (c == '-')
        s = -1, c = getChar();
    while ('0' <= c && c <= '9')
        x = x * 10 + c - '0', c = getChar();
    return s == 1 ? x : -x;
}
     
/** Write */
     
static int write_pos = 0;
static char write_buf[buf_size];
     
inline void writeChar( int x ) {
    if (write_pos == buf_size)
        fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
    write_buf[write_pos++] = x;
}
     
template <class T> 
inline void writeInt( T x, char end ) {
    if (x < 0)
        writeChar('-'), x = -x;
     
    char s[24];
    int n = 0;
    while (x || !n)
        s[n++] = '0' + x % 10, x /= 10;
    while (n--)
        writeChar(s[n]);
    if (end)
        writeChar(end);
}
     
inline void writeWord( const char *s ) {     while (*s)
writeChar(*s++); }
     
struct Flusher {
    ~Flusher() {
        if (write_pos)
            fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
    }
} flusher;
   
   
#define sz(c) (int)(c).size()
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define left left228
#define right right228
#define next next228
#define rank rank228
#define prev prev228
#define y1 y1228
const int MAXN = 100003;


int n, m;
int a[MAXN];
long long d1[4 * MAXN];
long long mod1[4 * MAXN];
int ss = 1;


inline void push1(int i) {
    if (mod1[i] != 0) {
        d1[i] += mod1[i];
        if (i < ss) {
            mod1[i * 2] += mod1[i];
            mod1[i * 2 + 1] += mod1[i];
        }
        mod1[i] = 0;
    }
}


void add1(int v, int vl, int vr, int l, int r, int x) {
    push1(v);
    if (l > vr || vl > r) {
        return;
    }
    if (l <= vl && vr <= r) {
        mod1[v] += x;
     //   cout << vl << ' ' << vr << ' ' << x << endl;
        push1(v);
        return;
    }
    add1(v * 2, vl, (vl + vr) / 2, l, r, x);
    add1(v * 2 + 1, (vl + vr) / 2 + 1, vr, l, r, x);
    d1[v] = max(d1[v * 2], d1[v * 2 + 1]);
}


long long rmq1(int v, int vl, int vr, int l, int r) {
    push1(v);
    if (l > vr || vl > r) {
        return -1e18;
    }
    if (l <= vl && vr <= r) {
        return d1[v];
    }
    return max(rmq1(v * 2, vl, (vl + vr) / 2, l, r), rmq1(v * 2 + 1, (vl + vr) / 2 + 1, vr, l, r));
}


int main() {
    n = readInt(), m = readInt();
    while (ss < m) {
        ss <<= 1;
    }
    for (int i = 0; i < n; i++) {
        a[i] = readInt();
    }
    for (int i = 0; i < n - 1; i++) {
        int l = a[i];
        int r = a[i + 1];
        if (l <= r) {
            l--, r--;
            if (l + 3 <= r + 1) {
                //cout << l + 2 << ' ' << r + 1 << endl;
                add1(1, 1, ss, l + 3, r + 1, 1);
                add1(1, 1, ss, r + 2, r + 2, -(r - l - 1));
            }
        } else {
            l--, r--;
            if (l + 3 <= m) {
                add1(1, 1, ss, l + 3, m, 1);
            }
            if (l + 1 == m) {
                if (2 <= r + 1) {
                    add1(1, 1, ss, 2, r + 1, 1);
                    add1(1, 1, ss, r + 2, r + 2, -r);
                }
            } else {
                if (l + 2 == m) {
                    add1(1, 1, ss, 1, r + 1, 1);
                    add1(1, 1, ss, r + 2, r + 2, -(r + 1));
                } else {
                    int x = m - l - 2;
                    add1(1, 1, ss, 1, 1, x);
                    add1(1, 1, ss, r + 2, r + 2, -x);
                    if (2 <= r + 1) {
                        add1(1, 1, ss, 2, r + 1, 1);
                        add1(1, 1, ss, r + 2, r + 2, -r);
                    }
                }
            }
        }
        // for (int j = 0; j < m; j++) {
        //     cout << rmq1(1, 1, ss, j + 1, j + 1) << ' ';
        // }
        // cout << endl;
    }
    long long cur = 0;
    int pos = 0;
    long long ans = 0;
    for (int i = 0; i < m; i++) {
        cur += rmq1(1, 1, ss, i + 1, i + 1);
      //  cout << cur << endl;
        if (ans < cur) {
            ans = cur;
            pos = i;
        }
    }
    //cout << pos << endl;
    long long sum = 0;
    for (int i = 0; i < n - 1; i++) {
        int l = a[i];
        int r = a[i + 1];
        l--, r--;
        if (l <= r) {
            if (pos > l && pos <= r) {
                sum += r - pos + 1;
            } else {
                sum += r - l;
            }
        } else {
            if (pos > l) {
                sum += m - pos + r + 1;
            } else {
                if (pos <= r) {
                    sum += r - pos + 1;
                } else {
                    sum += m - l + r;
                }
            }
        }
    }
    printf("%lld\n", sum);
    return 0;               
}