// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1 << 17;

struct Segt {

	ll sum[N << 1], add[N << 1];

	Segt() {

		memset(sum, 0, sizeof sum);

		memset(add, 0, sizeof add);

	}

	void pd(int i, int l) {

		if (add[i]) {

			sum[i] += add[i] * l;

			if (i < N) {

				add[i + i] += add[i];

				add[i + i + 1] += add[i];

			}

			add[i] = 0;

		}

	}

	void upd(int i, int a, int b, int l, int r, int v) {

		if (l <= a && b <= r) {

			add[i] += v;

			pd(i, b - a); return;

		}

		pd(i, b - a);

		if (r <= a || b <= l) return;

		int md = (a + b) >> 1;

		upd(i + i, a, md, l, r, v);

		upd(i + i + 1, md, b, l, r, v);

		sum[i] = sum[i + i] + sum[i + i + 1];

	}

	ll qry(int i, int a, int b, int l, int r) {

		pd(i, b - a);

		if (l <= a && b <= r) return sum[i];

		if (r <= a || b <= l) return 0;

		int md = (a + b) >> 1;

		ll L = qry(i + i, a, md, l, r);

		ll R = qry(i + i + 1, md, b, l, r);

		return L + R;

	}

} segt;

int n, m;

int a[N];

int main() {

	ios :: sync_with_stdio(0);

	cin >> n >> m;

	for (int i = 0; i < n; ++ i) cin >> a[i], -- a[i];

	ll tot = 0;

	for (int i = 0; i + 1 < n; ++ i) {

		if (a[i] < a[i + 1]) {

			tot += a[i + 1] - a[i];

			segt.upd(1, 0, N, a[i] + 2, a[i + 1] + 1, 1);

			segt.upd(1, 0, N, a[i + 1] + 1, a[i + 1] + 2, -(a[i + 1] - a[i] - 1));

		} else {

			tot += m - a[i] + a[i + 1];

			segt.upd(1, 0, N, a[i] + 2, m, 1);

			segt.upd(1, 0, N, 0, 1, m - a[i] - 1);

			segt.upd(1, 0, N, 1, a[i + 1] + 1, 1);

			segt.upd(1, 0, N, a[i + 1] + 1, a[i + 1] + 2, -(m - a[i] + a[i + 1] - 1));

		}

//		for (int j = 0; j < m; ++ j) cerr << segt.qry(1, 0, N, 0, j + 1) << " "; cerr << endl;

	}

	cerr << tot << endl;

	ll mx = 0;

	for (int i = 0; i < m; ++ i) {

		ll cur = segt.qry(1, 0, N, 0, i + 1);

//		cerr << i << ": " << cur << endl;

		mx = max(mx, cur);

	}

	cout << tot - mx << endl;

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 27.623640258892536x):
#include<cstdio>

#include<algorithm>

using namespace std;

#define ll long long

const int maxn=1e5+5;

const ll INF=0x3f3f3f3f3f3f3f3f;

int a[maxn];

struct node

{

    int l,r;

    ll diff,lz;

}tree[4*maxn];

void build(int l,int r,int cur)

{

    tree[cur].l=l;

    tree[cur].r=r;

    if(l==r)

        return;

    int mid=(l+r)>>1;

    build(l,mid,cur<<1);

    build(mid+1,r,cur<<1|1);

    return;

}

void push_up(int cur)

{

    tree[cur].diff=tree[cur<<1].diff+tree[cur<<1|1].diff;

    return;

}

void push_down(int cur)

{

    if(tree[cur].lz!=0)

    {

        tree[cur<<1].diff+=(tree[cur<<1].r-tree[cur<<1].l+1)*tree[cur].lz;

        tree[cur<<1].lz+=tree[cur].lz;

        tree[cur<<1|1].diff+=(tree[cur<<1|1].r-tree[cur<<1|1].l+1)*tree[cur].lz;

        tree[cur<<1|1].lz+=tree[cur].lz;

        tree[cur].lz=0;

    }

    return;

}

void add(int l,int r,ll val,int cur)

{

    if(l>r) return;

    if(tree[cur].l>=l&&tree[cur].r<=r)

    {

        tree[cur].diff+=(tree[cur].r-tree[cur].l+1)*val;

        tree[cur].lz+=val;

        return;

    }

    if(tree[cur].l>r||tree[cur].r<l)

        return;

    push_down(cur);

    add(l,r,val,cur<<1);

    add(l,r,val,cur<<1|1);

    push_up(cur);

    return;

}

ll diff0[maxn];

void together(int cur)

{

    if(tree[cur].l==tree[cur].r)

    {

        diff0[tree[cur].l]=tree[cur].diff;

        return;

    }

    push_down(cur);

    together(cur<<1);

    together(cur<<1|1);

    push_up(cur);

    return;

}

ll minn[maxn];

int main()

{

    int n,m;

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++)

        scanf("%d",&a[i]);

    build(0,m-1,1);

    ll base=0;

    for(int i=1;i<n;i++)

    {

        if(a[i]<a[i+1])

        {

            base+=(ll)(a[i+1]-a[i]);

            add(a[i]+1,a[i+1]-1,-1,1);

            add(a[i+1],a[i+1],a[i+1]-a[i]-1,1);

        }

        else

        {

            base+=(ll)(m-a[i]+a[i+1]);

            add(0,0,-(m-a[i]),1);

            add(1,a[i+1]-1,-1,1);

            add(a[i+1],a[i+1],m-a[i]+a[i+1]-1,1);

            add(a[i]+1,m-1,-1,1);

        }

    }

    together(1);

    minn[0]=base;

    for(int i=1;i<=m;i++)

        minn[i]=minn[i-1]+diff0[i-1];

    ll ans=INF;

    for(int i=1;i<=m;i++)

        ans=min(ans,minn[i]);

    printf("%lld\n",ans);

    return 0;

}

/*

 4 6

 1 5 1 4

 8 9 7 5 6 9

 

 9 9 8 7 6 5 4 3 2

 */