#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<vector>
#include<array>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
#include<cassert>
#include<functional>
#include<random>
#include<complex>
#include<bitset>
#include<chrono>
#include<boost/multiprecision/cpp_int.hpp>
#define int int64_t
#define uint uint64_t
#define REP(i, a, b) for (int64_t i = (int64_t)(a); i < (int64_t)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i: a)
#define ITR(x, a) for (auto x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define Min(x) *min_element(ALL(x))
#define Max(x) *max_element(ALL(x))
#define Unique(L) (L.erase(unique(ALL(L)), L.end()))
#define veccat(v1, v2) std::copy((v2).begin(),(v2).end(),std::back_inserter(v1)/*v1の後ろにv2を入れる*/)
#define intmax (std::numeric_limits<int64_t>::max() / 4)
using namespace std;
typedef boost::multiprecision::cpp_int bigint;
const double EPS = 1e-9;
const double PI = acos(-1.0);


class modint {
	//MODが素数であることを前提として実装してあるが、その判定はしていない。
	//あまりが出るような除算をしてはいけない。
private:
	static const int MOD = 1000000007;
public:
	modint() {
		//assert(is_prime(MOD));
		this->number = 0;
	}
	modint(const int src) {
		//assert(is_prime(MOD));
		this->number = opposit(src);
	}
	modint(const modint &src) {
		this->number = src.number;
	}

	modint& operator += (const modint& obj) {
		this->number = san2(this->number + obj.number);
		return *this;
	}
	modint& operator -= (const modint& obj) {
		this->number = san2(this->number - obj.number + MOD);
		return *this;
	}
	modint& operator *= (const modint& obj) {
		this->number = (this->number * obj.number) % MOD;
		return *this;
	}
	modint& operator /= (const modint& obj) {
		this->number = (this->number * inverse(obj.number)) % MOD;
		return *this;
	}
	modint& operator += (const int n) {
		this->number = san2(this->number + opposit(n));
		return *this;
	}
	modint& operator -= (const int n) {
		this->number = san2(this->number - opposit(n) + MOD);
		return *this;
	}
	modint& operator *= (const int n) {
		this->number = (this->number * opposit(n)) % MOD;
		return *this;
	}
	modint& operator /= (const int n) {
		this->number = (this->number * inverse(n)) % MOD;
		return *this;
	}

	modint operator + (const modint obj) { modint re(*this); return re += obj; }
	modint operator - (const modint obj) { modint re(*this); return re -= obj; }
	modint operator * (const modint obj) { modint re(*this); return re *= obj; }
	modint operator / (const modint obj) { modint re(*this); return re /= obj; }
	modint operator + (const int n) { modint re(*this); return re += n; }
	modint operator - (const int n) { modint re(*this); return re -= n; }
	modint operator * (const int n) { modint re(*this); return re *= n; }
	modint operator / (const int n) { modint re(*this); return re /= n; }

	modint operator = (const int n) {
		this->number = opposit(n);
		return *this;
	}
	int get() {
		return number;
	}

private:
	int number;

	int opposit(int n) {
		if (n < 0)n = MOD - ((-n) % MOD);
		return n % MOD;
	}
	int inverse(int n) {
		n = opposit(n);
		int result = 1;
		for (int i = MOD - 2; i; i /= 2) {
			if (i % 2)result = (result * n) % MOD;
			n = (n * n) % MOD;
		}
		return result;
	}
	inline int san2(const int n) {
		return MOD <= n ? n - MOD : n;
	}
	bool is_prime(int n) {
		if (n <= 1)return false;
		if (n == 2)return true;
		if (n % 2 == 0) return false;
		const int upperbound = int(sqrt(n));
		for (int i = 3; i <= upperbound; i += 2) {
			if (n % i == 0) return false;
		}
		return true;
	}
};
struct combi {
public:
	vector<modint>facto;
	combi(const int N) :facto(N) {
		facto[0] = 1;
		REP(i, 1, N)facto[i] = facto[i - 1] * i;
	}
	modint get_combi(const int N, const int C) {
		//combination(N+C,C)を求めて返す。
		if (int(facto.size()) <= N + C) {
			int a = facto.size();
			facto.resize(N + C + 1);
			REP(i, a, N + C + 1)facto[i] = facto[i - 1] * i;
		}
		return facto[N + C] / (facto[N] * facto[C]);
	}
};


signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int n, m;
	cin >> n >> m;
	vector<int>a(n);
	rep(i, n)cin >> a[i];

	int naivecost = 0;
	vector<int>imos(m*3 + 1, 0);
	rep(i, n-1) {
		int nowpos = a[i];
		int nextpos = a[i + 1];

		if (nextpos > nowpos) {
			int dist = nextpos - nowpos;
			naivecost += dist;
			if (dist == 1)continue;
			imos[m+nextpos - dist + 2]++;
			imos[m + nextpos + 1] -= dist  - 2+ 2;
			imos[m + nextpos + 2] += dist -2+1;
		}
		else {
			int dist = nextpos - nowpos + m;
			naivecost += dist;
			if (dist == 1)continue;
			imos[m + nextpos - dist + 2]++;
			imos[m + nextpos + 1] -= dist  - 2+ 2;
			imos[m + nextpos +2] += dist -2+1;
		}


	}

	vector<int>imosdec(m * 3 + 1, 0);
	int num = 0, dif = 0;
	rep(i, m * 3 + 1) {
		num += dif;
		imosdec[i] = num;
		dif += imos[i];
	}

	vector<int>imosp(m + 1, 0);
	for (int i = 1; i <= m; i++)imosp[i] = imosdec[m + i] + imosdec[i] + imosdec[m + m + i];




	vector<pair<int,int>>benefit(m + 1);
	rep(i, m + 1)benefit[i] = make_pair(imosp[i], i);
	sort(ALL(benefit));
	int ansbene = benefit[m].first;


	cout << naivecost - ansbene << endl;




	return 0;
}





Measured memory_bytes: 8064