#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cassert>
#include <unordered_map>
#include <bitset>
#include <unordered_set>

using namespace std;

#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair
#define sz(a) (int)((a).size())
#ifdef _WIN32
#  define I64 "%I64d"
#else
#  define I64 "%lld"
#endif
#define fname "."

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair < int, int > pi;
typedef pair < int, ull > pu;

const int inf = (int)1e9 + 123;
const ll infl = (ll)1e18 + 123;
const double eps = 1e-9;

const int MAX_N = (int)1e5 + 5;
const int mod = (int)1e9 + 7;

int n, m;
int a[MAX_N];

vector < int > have[MAX_N];

ll now;

ll cnt[MAX_N], sum[MAX_N];

void init() {
    for (int i = 0; i < m; i++)
        sum[i] = 0, cnt[i] = 0;
}

void updateCnt(int l, int r, ll x) {
    assert(l <= r);
    cnt[l] += x, cnt[r + 1] -= x;
}

void updateSum(int l, int r, ll x) {
    assert(l <= r);
    sum[l] += x, sum[r + 1] -= x;
}

int dist(int x, int y) {
    return (y - x + m) % m;
}

void out() {
    ll f = 0, s = 0;
    for (int i = 0; i < m; i++) {
        f += cnt[i], s += sum[i];
        cout << now + f * 1ll * i + s << ' ';
    }
    cout << endl;
}

void updateLeft(int x, int sub, ll val) {
//    cout << "updateLeft " << x << ' ' << sub << " with " << val << endl;
    
    int l = (x - sub + m) % m, r = x;
    if (l <= r) {
        updateCnt(l, r, -1);
        updateSum(l, r, val + r);
    } else {
        updateCnt(0, r, -1);
        updateSum(0, r, val + r);
        
        updateCnt(l, m - 1, -1);
        updateSum(l, m - 1, val + m + r);
    }
}

void updateRight(int x, int add, ll val) {

//    cout << "updateRight " << x << ' ' << add << " with " << val << endl;

    
    int l = x, r = (x + add) % m;
    if (l <= r) {
        updateCnt(l, r, 1);
        updateSum(l, r, val - l);
    } else {
        updateCnt(l, m - 1, 1);
        updateSum(l, m - 1, val - l);
        
        updateCnt(0, r, 1);
        updateSum(0, r, val + m - l);
    }
}

int main() {
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        a[i]--;
    }
    for (int i = 2; i <= n; i++) {
        have[a[i]].pb(dist(a[i - 1], a[i]));
        now += dist(a[i - 1], a[i]);
    }
    init();
    
    int l, r;
    if (m % 2 == 1)
        l = m / 2, r = m / 2;
    else
        l = m / 2 - 1, r = m / 2;
    
    for (int i = 0; i < m; i++) {
        for (auto j : have[i]) {
            
//            cout << i << " and " << j << endl;
            
            int nwL = min(j - 1, m - 1), nwR = min(j - 1, r);
            updateLeft(i, nwL, 1 - j);
//            if (nwR > 0)
//                updateRight((i + 1) % m, nwR - 1, 2 - j);
            
//            out();
        }
    }

    ll ans = infl;
    
    for (int i = 0; i < m; i++) {
        if (i > 0)
            cnt[i] += cnt[i - 1], sum[i] += sum[i - 1];
        
        ans = min(ans, now + 1ll * i * cnt[i] + sum[i]);
    }
    
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 6912