#include <bits/stdc++.h>

#define null               ""
#define endl               '\n'
#define all(dat)           dat.begin(), dat.end()
#define loop(i, to)        for (num i = 0; i < to; ++i)
#define cont(i, to)        for (num i = 1; i <= to; ++i)
#define circ(i, fr, to)    for (num i = fr; i <= to; ++i)
#define foreach(i, dat)    for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long          num;

using namespace std;

const num nsz = 1e5, inf = 9e18;
num n, m, ans = inf;

struct segment_tree {
    num tr[4 * nsz + 5], fi[4 * nsz + 5], del[4 * nsz + 5];
    
    void inline up(num u) {
        tr[u] = tr[u << 1] + tr[u << 1 | 1];
    }
    
    void inline down(num u, num l, num r) {
        if (!fi[u] && !del[u])  return;
        num ls = u << 1, rs = u << 1 | 1, mid = (l + r) >> 1;
        num fl = fi[u], fr = fi[u] + (mid - l + 1) * del[u], el = fl + (mid - l) * del[u], er = fr + (r - mid - 1) * del[u];
        tr[ls] += (fl + el) * (mid - l + 1) / 2;
        tr[rs] += (fr + er) * (r - mid) / 2;
        fi[ls] += fi[u];
        fi[rs] += fi[u] + (mid - l + 1) * del[u];
        del[ls] += del[u];
        del[rs] += del[u];
        fi[u] = del[u] = 0;
    }
    
    void upd(num el, num er, num f, num de, num u = 1, num l = 1, num r = m) {
        if (l >= el && r <= er) {
            tr[u] += (f + f + (r - l) * de) * (r - l + 1) / 2;
            fi[u] += f;
            del[u] += de;
            return;
        }
        down(u, l, r);
        num mid = (l + r) >> 1;
        if (el <= mid) {
            upd(el, er, f, de, u << 1, l, mid);
        }
        if (er >= mid + 1) {
            upd(el, er, f + (mid - l + 1) * de, de, u << 1 | 1, mid + 1, r);
        }
        up(u);
    }
    
    num qry(num el, num er, num u = 1, num l = 1, num r = m) {
        if (l >= el && r <= er)  return tr[u];
        down(u, l, r);
        num mid = (l + r) >> 1, res = 0;
        if (el <= mid) {
            res += qry(el, er, u << 1, l, mid);
        }
        if (er >= mid + 1) {
            res += qry(el, er, u << 1 | 1, mid + 1, r);
        }
        return res;
    }
};
segment_tree tr;

num inline read() {
    num f = 1, res = 0;
    char c = getchar();
    for (; !isdigit(c); f = c == '-' ? -1 : 1, c = getchar());
    for (; isdigit(c); res *= 10, res += c ^ 48, c = getchar());
    return f * res;
}

num inline calc(num l, num r) {
    return l > r ? r + m - l : r - l;
}

bool inline chk(num pos) {
    return pos >= 1 && pos <= m;
}

int main() {
    n = read();
    m = read();
    num pre = read(), cur;
    cont (i, n - 1) {
        cur = read();
        num mx = calc(pre, cur), pos = cur, l = 1, r = pos - mx + 1, f;
//        cout << "mx: " << mx << endl;
//        cout << 1 << ' ' << l << ' ' << r << endl;
        if (l <= r && chk(l) && chk(r)) {
            tr.upd(l, r, mx, 0);
        }
        l = pos + 1;
        r = min(m, pos + m - mx + 1);
//        cout << 2 << ' ' << l << ' ' << r << endl;
        if (l <= r && chk(l) && chk(r)) {
            tr.upd(l, r, mx, 0);
        }
        l = max((num) 1, pos - mx + 2);
        r = pos;
        f = 1 + (pos - l);
//        cout << 3 << ' ' << l << ' ' << r << endl;
        if (l <= r && chk(l) && chk(r)) {
            tr.upd(l, r, f + (l - 1), -1);
        }
        l = pos + m - mx + 2;
        r = m;
        f = mx - 1;
//        cout << 4 << ' ' << l << ' ' << r << endl;
        if (l <= r && chk(l) && chk(r)) {
            tr.upd(l, r, f + (l - 1), -1);
        }
//        cont (i, m) {
//            cout << tr.qry(i, i) << ' ';
//        }
//        cout << endl;
        pre = cur;
    }
    cont (i, m) {
//        cout << tr.qry(i, i) << ' ';
        ans = min(ans, tr.qry(i, i));
    }
    cout << ans << endl;
}

//4 6
//1 5 1 4

//5 5
//1 3 1 5 1


Measured memory_bytes: 8576