// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



typedef long long ll;

typedef vector<int> vi;



#define Forn(i, n) for(int i=1; i<=(n); i++)

#define forn(i, n) for(int i=0; i<(n); i++)

#define lid id<<1|1

#define rid (id<<1)+2

#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)

#define pb push_back

#define mp make_pair

#define all(x) (x).begin(), (x).end()

#define mid l+r>>1

#define lson lid, l, mid

#define rson rid, mid, r 

#define f first

#define s second

#define int long long



const int MAX=1e5+5;



int N, M, ptr, ret, a[MAX];



struct Seg

{

    int tag1[MAX*4], tag2[MAX*4];

    Seg()

    {

        memset(tag1, 0, sizeof(tag1));

        memset(tag2, 0, sizeof(tag2));

    }

    inline void update(int id, int l, int r, int a, int b, int k1, int b1)

    {

       if(r<=a||b<=l) return;

       if(a<=l&&r<=b)

       {

           tag1[id]+=k1;

           tag2[id]+=b1;

           return;

       }

       update(lson, a, b, k1, b1);

       update(rson, a, b, k1, b1);

    }

    inline void update(int l, int r, int k1, int b1)

    {

        if(l>=r) return;

        update(0, 0, ptr, l, r, k1, b1);

    }

    inline void print(int id, int l, int r, int &ret)

    {

        if(l+1==r)

        {

            if(l) ret=min(ret, tag1[id]*l+tag2[id]);

            return;

        }

        

        tag1[lid]+=tag1[id];

        tag2[lid]+=tag2[id];

        

        tag1[rid]+=tag1[id];

        tag2[rid]+=tag2[id];

        

        print(lson, ret);

        print(rson, ret);

    }

}S;



signed main()

{

    scanf("%lld %lld", &N, &M);

    forn(i, N)

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

    ptr=M+1;

    

    forn(i, N-1)

    {

        if(a[i]==a[i+1]) continue;

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

        {

            S.update(1, a[i]+1, 0, a[i+1]-a[i]);

            S.update(a[i]+1, a[i+1]+1, -1, a[i+1]+1);

            S.update(a[i+1]+1, ptr, 0, a[i+1]-a[i]);

        }

        else

        {

            S.update(1, a[i+1]+1, -1, a[i+1]+1);

            S.update(a[i+1]+1, a[i]+1, 0, M-a[i]+a[i+1]);

            S.update(a[i]+1, ptr, -1, M+a[i+1]+1);

        }

    }

    ret=2e18;

    S.print(0, 0, ptr, ret);

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

    return 0;

}

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

}