//This is getting accepted!
// I HATE BUG
// God Of The Bugs
// 12/11/2016
#include<bits/stdc++.h>

using namespace std;

#define ms(s, n) memset(s, n, sizeof(s))
#define FI first
#define SE second
#define pb push_back
#define mp make_pair
#define ll long long
#define sz(a) ((int)(a).size())
#define __builtin_popcount __builtin_popcounll
#define ld long double

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> ppi;

const double PI = acos(0) * 2;
const double EPS = 1e-8;
const ll MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const ll oo = 1e18;
const double foo = 1e30;

template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcounll(s);}
template<class T> T sqr(T x) { return x * x; }

inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}

int n, m, leaf[MAXN << 2], a[MAXN];
pair<ll, ll> _lz[MAXN << 2];
ll val[MAXN << 2];

void build(int node, int L, int R) {
//	if (L == 6 && R == 6) cout << node << endl;
//	if (node == 3) cout << L << " " << R << endl;
	val[node] = oo;
	_lz[L] = mp(0, 0);
	
	if (L == R) {
		val[node] = 0;
		leaf[node] = 1;
	}
	
	if (L < R) {
		int mid = (L + R) >> 1;
		build(node * 2, L, mid);
		build(node * 2 + 1, mid + 1, R);
	}
}

void pushdown(int node, int L, int R) {
	if (leaf[node]) {
		val[node] += _lz[node].FI;
		_lz[node] = mp(0, 0);
		return;
	}
	
	int mid = (L + R) >> 1;
	int rem = mid - L + 1;
	_lz[node * 2].FI += _lz[node].FI;
	_lz[node * 2].SE += _lz[node].SE;
	_lz[node * 2 + 1].FI += _lz[node].FI - _lz[node].SE * rem;
	_lz[node * 2 + 1].SE += _lz[node].SE;
	_lz[node] = mp(0, 0);
}

void update(int node, int L, int R, int l, int r, int v, int d) {
	if (l > R || r < L) return;
	pushdown(node, L, R);
	
	if (l <= L && R <= r) {
		int rem = L - l;
		_lz[node].FI += (v - d * rem);
		_lz[node].SE += d;
		return;
	}
	
	if (L < R) {
		int mid = (L + R) >> 1;
		update(node * 2, L, mid, l, r, v, d);
		update(node * 2 + 1, mid + 1, R, l, r, v, d);
	}
}

void finalBuild(int node, int L, int R) {
	pushdown(node, L, R);
//	if (L == R) {
//		cout << L << " " << val[node] << endl;
//	}
	if (L < R) {
		int mid = (L + R) >> 1;
		finalBuild(node * 2, L, mid);
		finalBuild(node * 2 + 1, mid + 1, R);
		val[node] = min(val[node * 2], val[node * 2 + 1]);
	}
}

int main() {
//#ifndef ONLINE_JUDGE
//    freopen("inp.txt", "r", stdin);
////    freopen("out.txt", "w", stdout);
//#endif

	cin >> m >> n;
	for (int i=0; i<m; i++) cin >> a[i];
	
	build(1, 1, n);
	
	for (int i=0; i<m-1; i++) {
		if (a[i + 1] < a[i]) {
			update(1, 1, n, a[i] + 1, n, a[i + 1] + n - a[i], 1);
			update(1, 1, n, 1, a[i + 1], a[i + 1], 1);
			if (a[i + 1] + 1 <= a[i]) {
				update(1, 1, n, a[i + 1] + 1, a[i], a[i + 1] + n - a[i], 0);
			}
		}
		else {
			update(1, 1, n, a[i] + 1, a[i + 1], a[i + 1] - a[i], 1);
			update(1, 1, n, 1, a[i], a[i + 1] - a[i], 0);
			if (a[i + 1] + 1 <= n) {
				update(1, 1, n, a[i + 1] + 1, n, a[i + 1] - a[i], 0);
			}
		}
	}

	finalBuild(1, 1, n);
	
	cout << val[1];
	
}


Measured memory_bytes: 11008