#include <bits/stdc++.h>

using namespace std;

long long v[100005];
long long cnt[100005];
int a[100005];

int main(){
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i=1;i<=n;i++) scanf("%d", &a[i]);

	for(int i=2;i<=n;i++){
		if(a[i] > a[i-1]){
			// case 1
			int diff = a[i]-a[i-1];
			v[a[i]+1] += diff;
			v[1] += diff;
			v[a[i-1]+1] -= diff;

			// case 2
			v[a[i-1]+1] += diff+1;
			cnt[a[i-1]+1]++;
			v[a[i]+1]--;
			cnt[a[i]+1]--;
		}
		else{
			// case 1
			int diff = m-(a[i-1]-a[i]);
			v[a[i]+1] += diff;
			v[a[i-1]+1] -= diff;

			// case 2
			v[a[i-1]+1] += diff+1;
			cnt[a[i-1]+1]++;
			v[1] += diff - (m-a[i-1]) + 1;
			cnt[1]++;
			v[a[i]+1]--;
			cnt[a[i]+1]--;
		}
	}

	long long sum = 0;
	long long tot = 0;

	long long ans = 1e18;
	for(int i=1;i<=m;i++){
		sum += v[i];
		tot += cnt[i];
		sum -= tot;

		ans = min(ans, sum);
	}
	printf("%lld\n", ans);
}