// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int MAXSIZE=30000020;

const ll INF=0x3f3f3f3f3f3f3f3fLL;

int bufpos;

char buf[MAXSIZE];

#define NEG 0

void init(){

	#ifdef LOCAL

		freopen("C.txt","r",stdin);

	#endif

	buf[fread(buf,1,MAXSIZE,stdin)]='\0';

	bufpos=0;

}

#if NEG

int readint(){

	bool isneg;

	int val=0;

	for(;!isdigit(buf[bufpos]) && buf[bufpos]!='-';bufpos++);

	bufpos+=(isneg=buf[bufpos]=='-');

	for(;isdigit(buf[bufpos]);bufpos++)

		val=val*10+buf[bufpos]-'0';

	return isneg?-val:val;

}

#else

int readint(){

	int val=0;

	for(;!isdigit(buf[bufpos]);bufpos++);\

	for(;isdigit(buf[bufpos]);bufpos++)

		val=val*10+buf[bufpos]-'0';

	return val;

}

#endif

char readchar(){

	for(;isspace(buf[bufpos]);bufpos++);

	return buf[bufpos++];

}

int readstr(char* s){

	int cur=0;

	for(;isspace(buf[bufpos]);bufpos++);

	for(;!isspace(buf[bufpos]);bufpos++)

		s[cur++]=buf[bufpos];

	s[cur]='\0';

	return cur;

}

int x[100002],y[100002];

int a[100002];

int main(){

	init();

	int n=readint(),m=readint();

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

		a[i]=readint();

	int cnt=0;

	ll now=0,ans=INF;

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

		x[a[i]==m?1:a[i]+1]++,x[a[i+1]]--;

		int t=(a[i+1]-a[i]+m)%m;

		y[a[i+1]]+=t-1;

		now+=t;

		if (a[i]>a[i+1] && a[i]!=m)

			cnt++,now-=m-a[i]-1;

	}

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

		now-=cnt;

		//printf("%lld\n",now);

		ans=min(ans,now);

		now+=y[i];

		cnt+=x[i];

	}

	printf("%lld",ans);

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

using namespace std;

#define ll long long

const int maxn=1e5+5;

const ll INF=0x3f3f3f3f3f3f3f3f;

int a[maxn];

struct node

{

    int l,r;

    ll diff,lz;

}tree[4*maxn];

void build(int l,int r,int cur)

{

    tree[cur].l=l;

    tree[cur].r=r;

    if(l==r)

        return;

    int mid=(l+r)>>1;

    build(l,mid,cur<<1);

    build(mid+1,r,cur<<1|1);

    return;

}

void push_up(int cur)

{

    tree[cur].diff=tree[cur<<1].diff+tree[cur<<1|1].diff;

    return;

}

void push_down(int cur)

{

    if(tree[cur].lz!=0)

    {

        tree[cur<<1].diff+=(tree[cur<<1].r-tree[cur<<1].l+1)*tree[cur].lz;

        tree[cur<<1].lz+=tree[cur].lz;

        tree[cur<<1|1].diff+=(tree[cur<<1|1].r-tree[cur<<1|1].l+1)*tree[cur].lz;

        tree[cur<<1|1].lz+=tree[cur].lz;

        tree[cur].lz=0;

    }

    return;

}

void add(int l,int r,ll val,int cur)

{

    if(l>r) return;

    if(tree[cur].l>=l&&tree[cur].r<=r)

    {

        tree[cur].diff+=(tree[cur].r-tree[cur].l+1)*val;

        tree[cur].lz+=val;

        return;

    }

    if(tree[cur].l>r||tree[cur].r<l)

        return;

    push_down(cur);

    add(l,r,val,cur<<1);

    add(l,r,val,cur<<1|1);

    push_up(cur);

    return;

}

ll diff0[maxn];

void together(int cur)

{

    if(tree[cur].l==tree[cur].r)

    {

        diff0[tree[cur].l]=tree[cur].diff;

        return;

    }

    push_down(cur);

    together(cur<<1);

    together(cur<<1|1);

    push_up(cur);

    return;

}

ll minn[maxn];

int main()

{

    int n,m;

    scanf("%d%d",&n,&m);

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

        scanf("%d",&a[i]);

    build(0,m-1,1);

    ll base=0;

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

    {

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

        {

            base+=(ll)(a[i+1]-a[i]);

            add(a[i]+1,a[i+1]-1,-1,1);

            add(a[i+1],a[i+1],a[i+1]-a[i]-1,1);

        }

        else

        {

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

            add(0,0,-(m-a[i]),1);

            add(1,a[i+1]-1,-1,1);

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

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

        }

    }

    together(1);

    minn[0]=base;

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

        minn[i]=minn[i-1]+diff0[i-1];

    ll ans=INF;

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

        ans=min(ans,minn[i]);

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

    return 0;

}

/*

 4 6

 1 5 1 4

 8 9 7 5 6 9

 

 9 9 8 7 6 5 4 3 2

 */


// Fastest known solution for this problem: