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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, a, b) for (int i = (a); i < (b); i++)
//#include "library/debug.cpp"

template <class T>
struct LazySegmentTree {
    int n;
    T def;
    vector<T> node, lazy;
    vector<bool> flag;
    function<T(T, T)> func;

    LazySegmentTree(int _n, T _def, function<T(T, T)> _func)
        : def(_def), func(_func) {
        n = 1;
        while (n < _n) n *= 2;
        node.resize(2 * n - 1, def);
        lazy.resize(2 * n - 1, 0);
        flag.resize(2 * n - 1, false);
    }

    void eval(int k, int l, int r) {
        if (flag[k]) {
            node[k] += lazy[k];
            if (r - l > 1) {
                lazy[k * 2 + 1] += lazy[k];
                lazy[k * 2 + 2] += lazy[k];
                flag[k * 2 + 1] = flag[k * 2 + 2] = true;
            }
            lazy[k] = 0;
            flag[k] = false;
        }
    }

    void update(int a, int b, T v, int k, int l, int r) {
        eval(k, l, r);
        if (b <= l || r <= a) return;
        if (a <= l && r <= b) {
            lazy[k] += v;
            flag[k] = true;
            eval(k, l, r);
        } else {
            update(a, b, v, 2 * k + 1, l, (l + r) / 2);
            update(a, b, v, 2 * k + 2, (l + r) / 2, r);
            node[k] = func(node[2 * k + 1], node[2 * k + 2]);
        }
    }

    T get(int a, int b, int k, int l, int r) {
        if (r < 0) r = n;
        eval(k, l, r);
        if (b <= l || r <= a) return def;
        if (a <= l && r <= b) return node[k];
        T vl = get(a, b, 2 * k + 1, l, (l + r) / 2);
        T vr = get(a, b, 2 * k + 2, (l + r) / 2, r);
        return func(vl, vr);
    }

    void update(int a, int b, T v) {
        update(a, b, v, 0, 0, n);
    }

    T get(int a, int b) {
        return get(a, b, 0, 0, n);
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    rep(i, 0, n) cin >> a[i];
    auto func = [](ll a, ll b) { return a + b; };
    LazySegmentTree<ll> st(2 * m + 5, 0, func);
    rep(i, 0, n - 1) {
        ll dif = (a[i + 1] - a[i] + m) % m;
        st.update(a[i] + 2, a[i] + dif + 1, -1);
        st.update(a[i] + dif + 1, a[i] + dif + 2, dif - 1);
    }
    vector<ll> c(m + 1);
    rep(i, 0, m) {
        int j = (a[i] - 1) % m + 1;
        c[j] += st.get(j, j + 1) + st.get(j + m, j + m + 1);
    }
    ll ax = 0, mn = 0, cnt = 1;
    rep(i, 2, m + 1) {
        ax += c[i];
        if (mn > ax) {
            mn = ax;
            cnt = i;
        }
    }
    ll ans = 0;
    rep(i, 0, n - 1) {
        ans += min((a[i + 1] - a[i] + m) % m, (a[i + 1] - cnt + m) % m + 1);
    }
    cout << ans << endl;
    return 0;
}