// Original C++ solution (slower):
#include <cstdio>

#include <iostream>

#include <cstring>

#include <string>

#include <cmath>

#include <algorithm>

#include <cstdlib>

#include <utility>

#include <map>

#include <stack>

#include <set>

#include <vector>

#include <queue>

#include <deque>

#define x first

#define y second

#define mp make_pair

#define pb push_back

#define LL long long

#define Pair pair<int,int>

#define LOWBIT(x) x & (-x)

using namespace std;



const int MOD=1e9+7;

const int INF=0x7ffffff;

const int magic=348;



int n,m;

int a[200048];

LL s1[200048],s2[200048];



int main ()

{

	int i;

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

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

	int from,to;

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

	{

		from=a[i];to=a[i+1];

		if (from>to) to+=m;

		s1[from+1]+=from+1;s1[to+1]-=(from+1);

		s2[from+1]++;s2[to+1]--;

	}

	//for (i=1;i<=m*2;i++) cout<<s1[i]<<' ';

	//cout<<endl;

	//for (i=1;i<=m*2;i++) cout<<s2[i]<<' ';

	//cout<<endl;

	for (i=2;i<=m*2;i++)

	{

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

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

	}

	//for (i=1;i<=m*2;i++) cout<<s1[i]<<' ';

	//cout<<endl;

	//for (i=1;i<=m*2;i++) cout<<s2[i]<<' ';

	//cout<<endl;

	LL ans=0,res,fans=1e16;

	for (i=1;i<=n-1;i++) if (a[i]<a[i+1]) ans+=a[i+1]-a[i]; else ans+=m-(a[i]-a[i+1]);

	//cout<<ans<<endl;

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

	{

		res=s2[i]*i+s2[i+m]*(i+m)-s1[i]-s1[i+m];

		if (ans-res<fans) fans=ans-res;

	}

	cout<<fans<<endl;

	return 0;

}

// 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: