// eddy1021
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define mod9 1000000009LL
#define mod7 1000000007LL
#define INF  1023456789LL
#define INF16 10000000000000000LL
#define eps 1e-9
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#ifndef ONLINE_JUDGE
#define debug(...) printf(__VA_ARGS__)
#else 
#define debug(...)
#endif
inline LL getint(){
  LL _x=0,_tmp=1; char _tc=getchar();    
  while( (_tc<'0'||_tc>'9')&&_tc!='-' ) _tc=getchar();
  if( _tc == '-' ) _tc=getchar() , _tmp = -1;
  while(_tc>='0'&&_tc<='9') _x*=10,_x+=(_tc-'0'),_tc=getchar();
  return _x*_tmp;
}
inline LL add( LL _x , LL _y , LL _mod = mod7 ){
  _x += _y;
  return _x >= _mod ? _x - _mod : _x;
}
inline LL sub( LL _x , LL _y , LL _mod = mod7 ){
  _x -= _y;
  return _x < 0 ? _x + _mod : _x;
}
inline LL mul( LL _x , LL _y , LL _mod = mod7 ){
  _x *= _y;
  return _x >= _mod ? _x % _mod : _x;
}
LL mypow( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 1LL;
  LL _ret = mypow( mul( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = mul( _ret , _a , _mod );
  return _ret;
}
LL mymul( LL _a , LL _x , LL _mod ){
  if( _x == 0 ) return 0LL;
  LL _ret = mymul( add( _a , _a , _mod ) , _x >> 1 , _mod );
  if( _x & 1 ) _ret = add( _ret , _a , _mod );
  return _ret;
}
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
#define Bye exit(0)
int __ = 1 , _cs;
/*********default*********/
#define N 101010
LL st[ N << 2 ] , a[ N << 2 ] , d[ N << 2 ];
#define L(X) (X<<1)
#define R(X) (1+(X<<1))
#define mid ((l+r)>>1)
LL Ar( LL a0 , LL nn , LL ad ){
  return ( a0 + a0 + ( nn - 1 ) * ad ) * nn / 2;
}
void push( int no , int l , int r ){
  if( a[ no ] == 0 and d[ no ] == 0 ) return;
  st[ L( no ) ] += Ar( a[ no ] , mid - l + 1 , d[ no ] );
  st[ R( no ) ] += Ar( a[ no ] + ( mid - l + 1 ) * d[ no ] , r - mid , d[ no ] );
  a[ L( no ) ] += a[ no ];
  a[ R( no ) ] += a[ no ] + ( mid - l + 1 ) * d[ no ];
  d[ L( no ) ] += d[ no ];
  d[ R( no ) ] += d[ no ];
  a[ no ] = d[ no ] = 0;
}
void modify( int no , int l , int r , int ql , int qr , LL qa , LL qd ){
  if( l == ql and r == qr ){
    st[ no ] += Ar( qa , r - l + 1 , qd );
    a[ no ] += qa;
    d[ no ] += qd;
    return;
  }
  push( no , l , r );
  if( qr <= mid )
    modify( L( no ) , l , mid , ql , qr , qa , qd );
  else if( mid < ql )
    modify( R( no ) , mid + 1 , r , ql , qr , qa , qd );
  else{
    modify( L( no ) , l , mid , ql , mid , qa , qd );
    modify( R( no ) , mid + 1 , r , mid + 1 , qr , qa + ( mid - ql + 1 ) * qd , qd );
  }
  st[ no ] = st[ L( no ) ] + st[ R( no ) ];
}
LL query( int no , int l , int r , int p ){
  if( l == r ) return st[ no ];
  push( no , l , r );
  if( p <= mid ) return query( L( no ) , l , mid , p );
  return query( R( no ) , mid + 1 , r , p );
}
void build(){

}
LL n , m , aa[ N ];
void init(){
  n = getint();
  m = getint();
  for( int i = 1 ; i <= n ; i ++ )
    aa[ i ] = getint();
}
LL ans;
void go( int from , int to ){
  if( from == to ) return;
  LL tans = to - from;
  if( tans < 0 ) tans += m;
  ans += tans;
  from ++;
  if( from == to ) return;
  if( from > m ) from -= m;
  if( from <= to ){
    modify( 1 , 1 , m , from , to , 0 , 1 );
  }else{
    modify( 1 , 1 , m , from , m , 0 , 1 );
    //printf( "%d %lld + %d %d\n" , from , m , 0 , 1 );
    modify( 1 , 1 , m , 1 , to , m - from + 1 , 1 );
    //printf( "%d %d + %lld %d\n" , 1 , to , m - from + 1 , 1 );
  }
}
void solve(){
  for( int i = 2 ; i <= n ; i ++ )
    go( aa[ i - 1 ] , aa[ i ] );
  LL bst = 0;
  for( int i = 1 ; i <= m ; i ++ ){
    bst = max( bst , query( 1 , 1 , m , i ) );
    //cout << i << " " << query( 1 , 1 , m , i ) << endl;
  }
  printf( "%lld\n" , ans - bst );
}
int main(){
  build();
  //__ = getint();
  while( __ -- ){
    init();
    solve();
  }
}


Measured memory_bytes: 10496