#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define all(x) x.begin(),x.end()
#define ln '\n'
const double PI = acos(-1.0);
const long long MOD = 1000000007LL;
//const long long MOD = 998244353LL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////

template<typename T>
struct LazySegmentTree {
    int N,h;
    vector<T> node,lazy;

    LazySegmentTree() = default;
    LazySegmentTree(vector<T> &v) {init(v);}

    void init(vector<T> &v) {
        int sz = v.size();
        N = 1; h = 0;
        while (N < sz) {
            N *= 2;
            h++;
        }
        node.assign(2*N,0); lazy.assign(2*N,0);
        for (int i = 0; i < sz; i++) node[i+N] = v[i];
        for (int i = N-1; i >= 1; i--) node[i] = node[i<<1|0] + node[i<<1|1];
    }

    void eval(int k) {
        if (lazy[k] != 0) {
            node[k] += lazy[k];
            if (k < N) {
                lazy[k<<1|0] += lazy[k]/2;
                lazy[k<<1|1] += lazy[k]/2;
            }
            lazy[k] = 0;
        }
    }

    void update(int l, int r, T val) { // [l,r) (0-indexed)
        l += N; r += N-1;
        for (int i = h; i > 0; i--) {
            eval(l>>i);
            eval(r>>i);
        }
        int a = l; int b = r++; T length = 1;
        while (l < r) {
            if (l & 1) lazy[l++] += length * val;
            if (r & 1) lazy[--r] += length * val;
            l >>= 1; r >>= 1; length <<= 1;
        }
        while (a >>= 1, b >>= 1, a) {
            if (lazy[a] == 0) {
                eval(a<<1|0); eval(a<<1|1);
                node[a] = node[a<<1|0] + node[a<<1|1];
            }
            if (lazy[b] == 0) {
                eval(b<<1|0); eval(b<<1|1);
                node[b] = node[b<<1|0] + node[b<<1|1];
            }
        }
    }

    T get(int l, int r) { // [l,r) (0-indexed)
        T res = 0;
        l += N; r += N-1;
        for (int i = h; i > 0; i--) {
            eval(l>>i); eval(r>>i);
        }
        r++;
        while (l < r) {
            if (l & 1) {
                eval(l);
                res += node[l++];
            }
            l >>= 1;
            if (r & 1) {
                eval(--r);
                res += node[r];
            }
            r >>= 1;
        }
        return res;
    }
};

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N,M; cin >> N >> M;
    vector<ll> A(N);
    rep(i,N) {
        cin >> A[i];
        --A[i];
    }

    vector<ll> B(2*M+1);
    LazySegmentTree<ll> seg(B);
    ll MAX = 0;
    rep(i,N-1) {
        int u = A[i];
        int v = A[i+1];
        if (v < u) v += M;
        MAX += v-u;
        seg.update(u+2,v+1,1);
        seg.update(v+1,v+2,u-v+1);
    }
    rep(i,2*M) {
        if (i) {
            ll val = seg.get(i-1,i);
            seg.update(i,i+1,val);
        }
    }
    ll ans = 0;
    rep(i,M) {
        chmax(ans,seg.get(i,i+1) + seg.get(M+i,M+i+1));
    }

    cout << MAX-ans << ln;
    
}

Measured memory_bytes: 12800