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

#include<stdio.h>

#include<vector>

#include<cmath>

#include<queue>

#include<string.h>

#include<map>

#include<set>

#include<algorithm>

#define ll long long

#define pi pair < ll,ll >

#define mp(a,b) make_pair(a,b)

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

#define N 300004

#define INF 1e16



using namespace std;



ll n,m,ar[N];

ll seg[4*N][2];

ll lazy[4*N][2];



void relax(ll low,ll high,ll pos,ll id)

{

    if(lazy[pos][id] != 0)

    {

        seg[pos][id] += lazy[pos][id];



        if(low != high)

        {

            lazy[pos*2+1][id] += lazy[pos][id];

            lazy[pos*2+2][id] += lazy[pos][id];

        }



        lazy[pos][id]=0;

    }



    return;

}



void update(ll low,ll high,ll pos,ll slow,ll shigh,ll val,ll id)

{

    relax(low,high,pos,id);

    if(slow > shigh)

        return;



    if(low >= slow && high <= shigh)

    {

        seg[pos][id] += val;



        if(low != high)

        {

            lazy[pos*2+1][id] += val;

            lazy[pos*2+2][id] += val;

        }



        return;

    }

    if(low > shigh || high < slow)

        return;



    ll mid = (low+high)/2;



    update(low,mid,pos*2+1,slow,shigh,val,id);

    update(mid+1,high,pos*2+2,slow,shigh,val,id);



    return;

}



ll getval(ll low,ll high,ll pos,ll slow)

{

    relax(low,high,pos,0);

    relax(low,high,pos,1);



    if(low == high && low == slow)

    {

        return seg[pos][0] + seg[pos][1]*slow;

    }

    if(low > slow || high < slow)

        return 0;



    ll mid = (low+high)/2;



    return getval(low,mid,pos*2+1,slow)+getval(mid+1,high,pos*2+2,slow);

}



int main()

{

    ios_base::sync_with_stdio(false);



    cin >> n >> m;



    rep(i,0,n)

        cin >> ar[i];



    rep(i,0,n-1)

    {

        ll arxi = ar[i];

        ll telos = ar[i+1];



        if(ar[i] == ar[i+1])

            continue;

        else if(ar[i] < ar[i+1])

        {

            update(0,m,0,1,ar[i],ar[i+1]-ar[i],0);

            update(0,m,0,ar[i+1]+1,m,ar[i+1]-ar[i],0);



            update(0,m,0,ar[i]+1,ar[i+1],-1,1);

            update(0,m,0,ar[i]+1,ar[i+1],ar[i+1]+1,0);

        }

        else

        {

            update(0,m,0,1,ar[i+1],-1,1);

            update(0,m,0,1,ar[i+1],ar[i+1]+1,0);



            update(0,m,0,ar[i]+1,m,-1,1);

            update(0,m,0,ar[i]+1,m,m+ar[i+1]+1,0);



            update(0,m,0,ar[i+1]+1,ar[i],m-ar[i]+ar[i+1],0);

        }

    }



    ll ans = INF;



    rep(i,1,m+1)

    {

        ll res = getval(0,m,0,i);



//        cout << i << "  " << res << endl;

        ans = min(ans,res);

    }



    cout << ans;



    return 0;

}




// Faster C++ solution for the same problem (measured speedup: 5.2527018988461895x):
#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

 */