// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <iostream>

#include <ctime>

#include <map>

#include <queue>

#include <cstdlib>

#include <string>

#include <climits>

#include <set>

#include <vector>

#define int long long

using namespace std;

inline int read(){

	int k=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}

	return k*f;

}

int n,m,a[100010],s1[100010],s2[100010];

signed main()

{

	n=read();m=read();int sum=0;

	for(int i=1;i<=n;i++)a[i]=read();

	for(int i=1;i<n;i++){

		if(a[i]<a[i+1]){

			sum+=a[i+1]-a[i];

			s1[a[i]+1]+=a[i]+1;

			s1[a[i+1]+1]-=a[i]+1;

			s2[a[i]+1]++;s2[a[i+1]+1]--;

		}else{

			sum+=m-a[i]+a[i+1];

			s1[a[i]+1]+=a[i]+1;

			s1[1]-=m-a[i]-1;

			s1[a[i+1]+1]+=m-a[i]-1;

			s2[a[i]+1]++;s2[1]++;s2[a[i+1]+1]--;

		}

	}

	int ans=1e18;

	for(int i=1;i<=m;i++){

		s1[i]+=s1[i-1],s2[i]+=s2[i-1];

		ans=min(ans,sum+s1[i]-i*s2[i]);

	}

	if(ans>sum)ans=sum;

	printf("%lld",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.190119598918842x):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;

typedef long long ll;

inline void read(ll &x){

    x=0;static char ch;static bool flag;flag = false;

    while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;

    while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;

}

#define rg register int

#define rep(i,a,b) for(rg i=(a);i<=(b);++i)

#define per(i,a,b) for(rg i=(a);i>=(b);--i)

const ll maxn = 100010;

ll a[maxn],d[maxn],n,m;

ll lazy[maxn<<2],tag[maxn<<2],num[maxn<<2];

void modify(ll rt,ll l,ll r,ll L,ll R,ll v){

    if(L <= l && r <= R){

        lazy[rt] += v;

        return ;

    }

    ll mid = l+r >> 1;

    if(L <= mid) modify(rt<<1,l,mid,L,R,v);

    if(R >  mid) modify(rt<<1|1,mid+1,r,L,R,v);

}

void insert(ll rt,ll l,ll r,ll L,ll R,ll s){

    if(L <= l && r <= R){

        tag[rt] += s;++ num[rt];

        return ;

    }

    ll mid = l+r >> 1;

    if(R <= mid) insert(rt<<1,l,mid,L,R,s);

    else if(L > mid) insert(rt<<1|1,mid+1,r,L,R,s);

    else{

        insert(rt<<1,l,mid,L,mid,(R-mid)+s);

        insert(rt<<1|1,mid+1,r,mid+1,R,s);

    }

}

ll res;

void query(ll rt,ll l,ll r,ll p){

    res += lazy[rt];

    res += tag[rt] + num[rt]*(r - p);

    if(l == r) return ;

    ll mid = l+r >> 1;

    if(p <= mid) query(rt<<1,l,mid,p);

    else query(rt<<1|1,mid+1,r,p);

}

int main(){

    read(n);read(m);

    rep(i,1,n){

        read(a[i]);

        if(i > 1){

            ll x = (a[i] - a[i-1] + m) % m;

            if(a[i] > a[i-1]){

                modify(1,1,m,1,a[i-1],x);

                if(a[i]+1 <= m) modify(1,1,m,a[i]+1,m,x);

                if(a[i-1]+1 <= a[i]) insert(1,1,m,a[i-1]+1,a[i],1);

            }else{

                modify(1,1,m,a[i]+1,a[i-1],x);

                insert(1,1,m,1,a[i],1);

                if(a[i-1]+1 <= m) insert(1,1,m,a[i-1]+1,m,a[i]+1);

            }

        }

    }

    ll ans = 1LL << 60;

    rep(i,1,m){

        res = 0;query(1,1,m,i);

        ans = min(ans,res);

    }

    printf("%lld\n",ans);

    return 0;

}