// 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: 5.258939264183325x):
#include<iostream>

#include<stdio.h>

#include<vector>

#include<cmath>

#include<queue>

#include<string.h>

#include<map>

#include<set>

#include<algorithm>

#define ll long long

#define pi pair < ll,ll >

#define mp(a,b) make_pair(a,b)

#define rep(i,a,b) for(int i = a;i < b;i++)

#define N 300004

#define INF 1e16



using namespace std;



ll n,m,ar[N];

ll seg[4*N][2];

ll lazy[4*N][2];



void relax(ll low,ll high,ll pos,ll id)

{

    if(lazy[pos][id] != 0)

    {

        seg[pos][id] += lazy[pos][id];



        if(low != high)

        {

            lazy[pos*2+1][id] += lazy[pos][id];

            lazy[pos*2+2][id] += lazy[pos][id];

        }



        lazy[pos][id]=0;

    }



    return;

}



void update(ll low,ll high,ll pos,ll slow,ll shigh,ll val,ll id)

{

    relax(low,high,pos,id);

    if(slow > shigh)

        return;



    if(low >= slow && high <= shigh)

    {

        seg[pos][id] += val;



        if(low != high)

        {

            lazy[pos*2+1][id] += val;

            lazy[pos*2+2][id] += val;

        }



        return;

    }

    if(low > shigh || high < slow)

        return;



    ll mid = (low+high)/2;



    update(low,mid,pos*2+1,slow,shigh,val,id);

    update(mid+1,high,pos*2+2,slow,shigh,val,id);



    return;

}



ll getval(ll low,ll high,ll pos,ll slow)

{

    relax(low,high,pos,0);

    relax(low,high,pos,1);



    if(low == high && low == slow)

    {

        return seg[pos][0] + seg[pos][1]*slow;

    }

    if(low > slow || high < slow)

        return 0;



    ll mid = (low+high)/2;



    return getval(low,mid,pos*2+1,slow)+getval(mid+1,high,pos*2+2,slow);

}



int main()

{

    ios_base::sync_with_stdio(false);



    cin >> n >> m;



    rep(i,0,n)

        cin >> ar[i];



    rep(i,0,n-1)

    {

        ll arxi = ar[i];

        ll telos = ar[i+1];



        if(ar[i] == ar[i+1])

            continue;

        else if(ar[i] < ar[i+1])

        {

            update(0,m,0,1,ar[i],ar[i+1]-ar[i],0);

            update(0,m,0,ar[i+1]+1,m,ar[i+1]-ar[i],0);



            update(0,m,0,ar[i]+1,ar[i+1],-1,1);

            update(0,m,0,ar[i]+1,ar[i+1],ar[i+1]+1,0);

        }

        else

        {

            update(0,m,0,1,ar[i+1],-1,1);

            update(0,m,0,1,ar[i+1],ar[i+1]+1,0);



            update(0,m,0,ar[i]+1,m,-1,1);

            update(0,m,0,ar[i]+1,m,m+ar[i+1]+1,0);



            update(0,m,0,ar[i+1]+1,ar[i],m-ar[i]+ar[i+1],0);

        }

    }



    ll ans = INF;



    rep(i,1,m+1)

    {

        ll res = getval(0,m,0,i);



//        cout << i << "  " << res << endl;

        ans = min(ans,res);

    }



    cout << ans;



    return 0;

}