#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define mp make_pair
#define fs first
#define se second
#define pi 2*acos(0)
#define PI 3.14159265358979323846264338

typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;
const int N = 100010;
const int mod = (int)1e9 + 7;

inline int in() {int x; scanf("%d",&x); return x;}
inline ll lin() {ll x; scanf("%lld",&x); return x;}

int fx[]={1,-1,0,0};
int fy[]={0,0,-1,1};

int inp[N];
int bad[N];

struct SegTree {
    ll sum, add;
    int flag, on;
}t[N * 4];

void build(int x, int b, int e){
    if(b == e){
        t[x].sum = 0;
        t[x].add = 0;
        t[x].flag = 0;
        return ;
    }
    int m = (b + e) >> 1;
    build(x + x, b, m);
    build(x + x + 1, m + 1, e);
    t[x].sum = t[x].add = t[x].flag = 0;
}

void modify_sum(int x, int b, int e, int l, int r, int v){
    if(b > r || e < l) return ;
    if(b >= l && e <= r){
        t[x].sum += v;
        t[x].flag = 1;
        return ;
    }
    int m = (b + e) >> 1;
    modify_sum(x + x, b, m, l, r, v);
    modify_sum(x + x + 1, m + 1, e, l, r, v);
}

void modify_add(int x, int b, int e, int l, int r, int v){
    if(b > r || e < l) return ;
    if(b >= l && e <= r){
        t[x].add += v;
        t[x].on = 1;
        return ;
    }
    int m = (b + e) >> 1;
    modify_add(x + x, b, m, l, r, v);
    modify_add(x + x + 1, m + 1, e, l, r, v);
}

void lazy_add(int x, int b, int e){
    if(b != e){
        if(t[x].on > 0){
            t[x + x].add += t[x].add;
            t[x + x + 1].add += t[x].add;
            t[x].add = 0;
            t[x].on = 0;
            t[x + x].on = 1;
            t[x + x + 1].on = 1;
        }
    }
}

ll get_add(int x, int b, int e, int l, int r){
    lazy_add(x, b, e);
    if(b > r || e < l) return 0;
    if(b >= l && e <= r){
        return t[x].add;
    }
    int m = (b + e) >> 1;
    return get_add(x + x, b, m, l, r) +
           get_add(x + x + 1, m + 1, e, l, r);
}

void lazy_sum(int x, int b, int e){
    if(b != e){
        if(t[x].flag > 0){
            t[x + x].sum += t[x].sum;
            t[x + x + 1].sum += t[x].sum;
            t[x].sum = 0;
            t[x].flag = 0;
            t[x + x].flag = 1;
            t[x + x + 1].flag = 1;
        }
    }
}

ll get_sum(int x, int b, int e, int l, int r){
    lazy_sum(x, b, e);
    if(b > r || e < l) return 0;
    if(b >= l && e <= r){
        return t[x].sum;
    }
    int m = (b + e) >> 1;
    return get_sum(x + x, b, m, l, r) +
           get_sum(x + x + 1, m + 1, e, l, r);
}

int main(){
    int n = in(), m = in();
    for(int i = 1; i <= n; i++){
        inp[i] = in();
        if(i < n) bad[inp[i]]++;
    }
    build(1, 1, m);
    for(int i = 2; i <= n; i++){
        if(inp[i - 1] < inp[i]){
            int l = inp[i - 1], r = inp[i];
            modify_sum(1, 1, m, l, r, l);
            modify_add(1, 1, m, l, r, 1);
            //modify_add(1, 1, m, l, l, -1);
        }else {
            int l = inp[i - 1], r = m;
            modify_sum(1, 1, m, l, r, l);
            modify_add(1, 1, m, l, r, 1);
            //modify_add(1, 1, m, l, l, -1);
            l = 1; r = inp[i];
            modify_sum(1, 1, m, l, r, inp[i - 1] - m);
            modify_add(1, 1, m, l, r, 1);
        }
    }
    ll ans = 0, tot = 0;
    for(int i = 2; i <= n; i++){
        if(inp[i - 1] < inp[i]) ans += inp[i] - inp[i - 1];
        else {
            ans += m - inp[i - 1];
            ans += inp[i];
        }
    }
    tot = ans; //cout << ans << '\n';
    for(int i = 1; i <= m; i++){
        ll p = get_add(1, 1, m, i, i);
        ll q = get_sum(1, 1, m, i, i);
        //cout << p << " " << q << '\n';
        if(p > 0){
            ll tmp = tot - p * i + q + p - bad[i];
            ans = min(ans, tmp);
            //cout << i << " " << tmp << '\n';
        }
    }
    printf("%lld\n", ans);
}


Measured memory_bytes: 9216