#include <stdio.h>
#include <iostream>
#include <cstdio>
#include <algorithm> 
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <list>
#include <utility>
#include <set>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <bitset>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <random>

using namespace std;

static const double EPS = 1e-9;
template<class T> bool INRANGE(T x, T a, T b) { return a <= x&&x <= b; }
template<class T> void amin(T &a, T v) { if (a > v) a = v; }
template<class T> void amax(T &a, T v) { if (a < v) a = v; }
int ROUND(double x) { return (int)(x + 0.5); }
bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }
bool ISEQUAL(double x, double y) { return fabs(x - y) <= EPS*max(1.0, max(fabs(x), fabs(y))); }
double SQSUM(double x, double y) { return x*x + y*y; }
#define PI  (acos(-1))
#define ARRAY_NUM(a) (sizeof(a)/sizeof(a[0])) 
#define NG (-1)
#define BIG ((int)1e9+10)
#define BIGLL ((ll)4e18)
#define SZ(a) ((int)(a).size())
#define SQ(a) ((a)*(a))
typedef unsigned long long ull;
typedef long long ll;

#if 1

int main()
{
	int n, m;
	cin >> n >> m;

	vector <ll> a(n);
	vector <ll> b(2*m);

	ll sum = 0;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
		a[i]--;
		if (i > 0)
		{
			sum += (a[i] - a[i-1] + m) % m;

			int st = a[i - 1];
			int gl = a[i];
			if (st > gl)
			{
				gl += m;
			}

			b[st + 2] += 1;
			b[gl + 1] -= 1;
		}
	}

	for (int i = 1; i < 2 * m; i++)
	{
		b[i] += b[i - 1];
	}

	for (int i = 0; i < n; i++)
	{
		if (i > 0)
		{
			int st = a[i - 1];
			int gl = a[i];
			if (st > gl)
			{
				gl += m;
			}

			b[gl + 1] -= gl-st-1;
		}
	}

	for (int i = 1; i < 2 * m; i++)
	{
		b[i] += b[i - 1];
	}


	vector <ll> buttons(m);
	for (int i = 0; i < m; i++)
	{
		buttons[i] = sum - (b[i] + b[i+m]);
	}

	ll result = *min_element(buttons.begin(), buttons.end());

	cout << result << endl;
	return 0;
}

#elif 1

// MODの素数以外でも使えるバージョン。階乗の逆元を求める。
ll extgcd(ll a, ll b, ll& x, ll& y)
{
	ll d = a;
	if (b != 0)
	{
		d = extgcd(b, a%b, y, x);
		y -= (a / b)*x;
	}
	else
	{
		x = 1;
		y = 0;
	}
	return d;
}

// 逆元を求める
ll mod_inverse(ll a, ll m)
{
	ll x, y;
	extgcd(a, m, x, y);
	return (m + x%m) % m;
}

enum { NUM_FACTORIAL = 100005 };
ll f[NUM_FACTORIAL];
ll f_inv[NUM_FACTORIAL];
ll MOD = (ll)1e9 + 7LL;

void mod_factorial_init()
{
	f[0] = 1;
	for (int i = 1; i < NUM_FACTORIAL; ++i)
	{
		f[i] = f[i - 1] * i;
		f[i] %= MOD;
	}

	for (int i = 0; i < NUM_FACTORIAL; ++i)
	{
		f_inv[i] = mod_inverse(f[i], MOD);
	}
}

ll factorial_comb(int n,int r)
{
	if (r > n)
	{
		return 0;
	}

	ll ret = f[n];
	ret *= f_inv[r];
	ret %= MOD;
	ret *= f_inv[n-r];
	ret %= MOD;
	return ret;
}


int main()
{
	int N;
	cin >> N;
	mod_factorial_init();

	vector <int> place(N+1, NG);

	int haji = 0;
	for (int i = 0; i < N+1; ++i)
	{
		int tmp;
		cin >> tmp;
		if (place[tmp] == NG)
		{
			place[tmp] = i;
		}
		else
		{
			int st = i;
			int ed = place[tmp];
			if(st>ed) swap(st,ed);

			haji = st + (N-ed);
		}
	}

	for(int k=1;k<=N+1;k++)
	{
		ll a = factorial_comb(N+1, k);
		ll b = factorial_comb(haji, k-1);
		ll ans = a - b;

		ans = ((ans%MOD)+MOD) % MOD;
		printf("%lld\n",ans);
	}

	return 0;
}


#else

int main()
{
	int N;
	cin >> N;



	vector <int> a(N);
	for (int i = 0; i < N; ++i)
	{
		cin >> a[i];
//		a[i] = i;
	}

	vector <int> b;
	for (int i = N-1; i >= 0; i-=2)
	{
		b.push_back(a[i]);
	}

	for (int i = N%2; i < N; i += 2)
	{
		b.push_back(a[i]);
	}

	for (int i = 0; i < N; ++i)
	{
		printf("%d ",b[i]);
	}
	printf("\n");

	return 0;
}

#endif

Measured memory_bytes: 3328