//#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast,no-stack-protector") 
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

#define int long long

#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp(a, b) make_pair(a, b)
#define ret(a) {cout << a; exit(0);}

using namespace std;
                                                                                                      
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

typedef pair < int , int > pii ;
typedef pair < long long  , long long > pll ;

const int N = 2e5 + 123;
#define ln sddfsf
int n , m , k , a[N] , ln[N];
vector < int > t[N << 2] , sum[N << 2];
vector < pii >  sg;
void add(int x , int y , int val){
	ln[x] += val;
	ln[y + 1] -= val;
}
void upd(int v  , int tl, int tr , int pos , int x){
	t[v].pb(x);
	if(tl == tr)return;
	int tm = (tl + tr) >> 1;
	if(pos <= tm)
		upd(v << 1,  tl , tm , pos , x);
	else 
		upd(v << 1|1, tm + 1,  tr , pos , x);
}
pii comb(pii a , pii b){
	return {a.fi + b.fi , b.se + a.se};
}
pii flex(vector < int > &a , vector < int > &b , int x , int lim){
	if(sz(a) == 0)return {0,0};
	int l = 0 , r = sz(a) - 1 , p = sz(a) - 1 , p2 = sz(a) - 1;
    if(a[r] < x or a[0] > lim)
		return {0,0};
	while(l <= r){
		int m = (l + r) >> 1;
		if(a[m] >= x)
			p = m , r = m - 1;
		else l = m + 1;			
	}
	l = 0 , r = sz(a) - 1;
	while(l <= r){
		int m = (l + r) >> 1;
		if(a[m] <= lim)
			p2 = m , l = m + 1;
		else r = m - 1;			
	}

	if(p > p2)return{0,0};
	int sum = b[p];
	if(p2 != sz(a) - 1)sum -= b[p2 + 1];
	return {sum , p2 - p + 1};
}

pii get(int v , int tl , int tr , int l , int r , int x , int lim){
	if(l > r or l > tr or tl > r)
		return {0,0};
	if(l <= tl and tr <= r){
		return flex(t[v] , sum[v] , x , lim);		
	}
	int tm = (tl + tr) >> 1;
	return comb(get(v << 1, tl , tm , l , r , x, lim ) , get(v << 1|1 , tm + 1, tr , l , r , x , lim));
}
void Add(int x , int y){
	if(x < y){

		add(1 , x ,y - x);
		add(y + 1 , m, y - x);
		upd(1 , 1 , m + 1, x + 1 , y);
	}
    else {
		y += m;
        add(y - m + 1, x ,y - x);
		upd(1 , 1 , m + 1, x + 1 , y);
    }
}
main(){                                                                   	
	//12341234
	//12345678
	cin >> n >> m;
	for(int i = 1; i <= n ; i++){
		cin >> a[i];
		if(i > 1)
			Add(a[i - 1] , a[i]);
	}

	for(int i = 1 ; i <= 4 * (m + 1) ; i++){
		if(sz(t[i]) == 0)continue;
		sort(all(t[i]));
		sum[i].resize(sz(t[i]));
		sum[i][sz(t[i]) - 1] = t[i].back();
		for(int j = sz(t[i]) - 2; j >= 0 ; j--){
			sum[i][j] = t[i][j] + sum[i][j + 1];
		}
	}

	for(int i = 1 ; i <= m + m; i++)
		ln[i] += ln[i - 1];


	for(int i = 1;  i <= m; i++){
		auto tmp = get(1 , 1 , m + 1, 1 , i, i , m + m);
		ln[i] += tmp.fi - (i - 1)*tmp.se;
	}

	for(int i = m + 1 ; i <= 2 * m ; i++){
		auto tmp = get(1 , 1 , m + 1, 1 , m + 1 , i , m + m);
		ln[i - m] += ln[i] + tmp.fi - (i - 1)*tmp.se;
	}

	int ans = 0 , cost = 1e18;
	for(int i = 1 ; i <= m ; i++){
		if(ln[i] < cost)
        	cost = ln[i] , ans = i;
	}
	cout << ln[ans] ;		
}


Measured memory_bytes: 76524