/*
СТРОИМ СТЕНУ РАБОТЯГИ!
█▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
█═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═█
█═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═█
█═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═█
█═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═█
█═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═█
█═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═█
█═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═█
█═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═╩═╦═█
█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█
*/
#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));
}


inline int dist(int pos, int r) {
    if (pos <= r) {
        return r - pos;
    } else {
        return m - pos + 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
                //r + 1 - l - 3
                //r - l - 2
                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 == m - 1) {
                if (2 <= r + 1) {
                    add1(1, 1, ss, 2, r + 1, 1);
                    add1(1, 1, ss, r + 2, r + 2, -r);
                }
            } else {
                if (l == m - 2) {
                    add1(1, 1, ss, 1, r + 1, 1);
                    add1(1, 1, ss, r + 2, r + 2, -(r + 1));
                } else {
                    int x = m - l - 1;
                    //m - (m - 3) - 2
                    //1
                    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);
        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--;
        sum += min(dist(l, r), dist(pos, r) + 1);
    }
    printf("%lld\n", sum);
    return 0;               
}                                     


Measured memory_bytes: 6400