#include <bits/stdc++.h>

#define __INIT_CC__ ios::sync_with_stdio(false); \
	cin.tie(0);

#ifdef __WIN32__
	char getchar_unlocked() {return getchar();}
#endif

#define FOR(_i,_n,_m) for(int (_i)=(_n),_t=(_m);_i<(_t);++(_i))
#define FORN(_i,_n,_m) for(int (_i)=(_n),_t=(_m);_i<=(_t);++(_i))
#define FORD(_i,_n,_m) for(int (_i)=(_n),_t=(_m);_i>=(_t);--(_i))
#define RESET(_a,_value) memset(_a,_value,sizeof(_a))
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define ff first
#define ss second
#define mp make_pair
#define SIZE(_a) (int)_a.size()
#define VSORT(_a) sort(_a.begin(),_a.end())
#define SSORT(_a,_val) sort(_a,_a+(_val))
#define ALL(_a) _a.begin(),_a.end()
#define mt make_tuple
#define _get(_tuple,_i) get<_i>(_tuple)
#define eb emplace_back
 
using namespace std;

using vi = vector<int>;
using ii = pair<int,int>;
using vii = vector<ii>;
using ll = long long;
using ull = unsigned long long;
using pll = pair<ll,ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using pdd = pair<double,double>;
using vpdd = vector<pdd>;
template <typename T> using uset = unordered_set<T>;
template <typename T> using umultiset = unordered_multiset<T>;
template <typename K, typename V> using umap = unordered_map<K, V>;
template <typename K, typename V> using umultimap = unordered_multimap<K, V>;

const int dr[] = { 1, 0,-1, 0, 1, 1,-1,-1};
const int dc[] = { 0, 1, 0,-1, 1,-1,-1, 1};
const double eps = 1e-9;
const int INF = 0x7FFFFFFF;
const ll INFLL = 0x7FFFFFFFFFFFFFFFLL;
const double pi = acos(-1);

template <typename T> T take(queue<T> &O) {T tmp=O.front();O.pop();return tmp;}
template <typename T> T take(stack<T> &O) {T tmp=O.top();O.pop();return tmp;}
template <typename T> T take(priority_queue<T> &O) {T tmp=O.top();O.pop();return tmp;}
template <typename T>
inline bool inRange(const T& z, const T& a, const T& b){return a <= z && z <= b;}

void OPEN(const string& in = "input.txt", const string& out = "output.txt")
{
	freopen(in.c_str(), "r", stdin);
	freopen(out.c_str(), "w", stdout);
	return ;
}

//using sokokaleb's template v4.0

struct STNode {
	ll a, b;
};

#define N 100005
STNode st[N << 2];
int n, m;
int lst, x;

void update(int node, int l, int r, int a, int b, ll aa, ll bb) {
	if (a <= l && r <= b) {
		st[node].a += aa;
		st[node].b += bb;
		return ;
	}
	int mid = (l + r) >> 1, A = node << 1, B = A | 1;
	if (mid >= a) {
		update(A, l, mid, a, b, aa, bb);
	}
	if (mid + 1 <= b) {
		int mv = (mid + 1) - max(a, l);
		int add = 0;
		if (mv > 0) {
			aa += bb * mv;
		}
		update(B, mid + 1, r, a, b, aa, bb);
	}
	return ;
}

ll ans = INFLL;

void dfs(int node, int l, int r) {
	if (l == r) {
		ans = min(ans, st[node].a);
		return ;
	}
	int mid = (l + r) >> 1, A = node << 1, B = A | 1;
	st[A].a += st[node].a;
	st[A].b += st[node].b;
	int mv = (mid + 1) - l;
	st[node].a += st[node].b * mv;
	st[B].a += st[node].a;
	st[B].b += st[node].b;

	dfs(A, l, mid);
	dfs(B, mid + 1, r);
	return ;
}

int main(int argc, char** argv) {
	__INIT_CC__
	cin >> n >> m;
	lst = -1;
	FOR (i, 0, n) {
		cin >> x;
		if (lst != -1) {
			if (lst == x) {
			} else if (lst < x) {
				update(1, 1, m, lst + 1, x, x - lst, -1);
				update(1, 1, m, 1, lst, x - lst, 0);
				if (x + 1 <= m) {
					update(1, 1, m, x + 1, m, x - lst, 0);
				}
			} else { // x < lst
				int def = x + (m - lst);
				if (lst + 1 <= m) {
					update(1, 1, m, lst + 1, m, def, -1);
				}
				update(1, 1, m, 1, x, x, -1);
				update(1, 1, m, x + 1, lst, def, 0);
			}
		}
		lst = x;
	}

	ans = INFLL;
	dfs(1, 1, m);
	cout << ans << "\n";
	return 0;
}


Measured memory_bytes: 6400