#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <functional>
#include <set>
#include <ctime>
#include <random>
#include <chrono>
#include <cassert>
#include <tuple>
#include <utility>
using namespace std;

namespace {
  using Integer = long long; //__int128;
  template<class T, class S> istream& operator >> (istream& is, pair<T,S>& p){return is >> p.first >> p.second;}
  template<class T> istream& operator >> (istream& is, vector<T>& vec){for(T& val: vec) is >> val; return is;}
  template<class T> istream& operator ,  (istream& is, T& val){ return is >> val;}
  template<class T, class S> ostream& operator << (ostream& os, const pair<T,S>& p){return os << p.first << " " << p.second;}
  template<class T> ostream& operator << (ostream& os, const vector<T>& vec){for(size_t i=0; i<vec.size(); i++) os << vec[i] << (i==vec.size()-1?"":" "); return os;}
  template<class T> ostream& operator ,  (ostream& os, const T& val){ return os << " " << val;}

  template<class H> void print(const H& head){ cout << head; }
  template<class H, class ... T> void print(const H& head, const T& ... tail){ cout << head << " "; print(tail...); }
  template<class ... T> void println(const T& ... values){ print(values...); cout << endl; }

  template<class H> void eprint(const H& head){ cerr << head; }
  template<class H, class ... T> void eprint(const H& head, const T& ... tail){ cerr << head << " "; eprint(tail...); }
  template<class ... T> void eprintln(const T& ... values){ eprint(values...); cerr << endl; }

  class range{ Integer start_, end_, step_; public: struct range_iterator{ Integer val, step_; range_iterator(Integer v, Integer step) : val(v), step_(step) {} Integer operator * (){return val;} void operator ++ (){val += step_;} bool operator != (range_iterator& x){return step_ > 0 ? val < x.val : val > x.val;} }; range(Integer len) : start_(0), end_(len), step_(1) {} range(Integer start, Integer end) : start_(start), end_(end), step_(1) {} range(Integer start, Integer end, Integer step) : start_(start), end_(end), step_(step) {} range_iterator begin(){ return range_iterator(start_, step_); } range_iterator   end(){ return range_iterator(  end_, step_); } };

  inline string operator "" _s (const char* str, size_t size){ return move(string(str)); }
  constexpr Integer my_pow(Integer x, Integer k, Integer z=1){return k==0 ? z : k==1 ? z*x : (k&1) ? my_pow(x*x,k>>1,z*x) : my_pow(x*x,k>>1,z);}
  constexpr Integer my_pow_mod(Integer x, Integer k, Integer M, Integer z=1){return k==0 ? z%M : k==1 ? z*x%M : (k&1) ? my_pow_mod(x*x%M,k>>1,M,z*x%M) : my_pow_mod(x*x%M,k>>1,M,z);}
  constexpr unsigned long long operator "" _ten (unsigned long long value){ return my_pow(10,value); }

  inline int k_bit(Integer x, int k){return (x>>k)&1;} //0-indexed

  mt19937 mt(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());

  template<class T> string join(const vector<T>& v, const string& sep){ stringstream ss; for(size_t i=0; i<v.size(); i++){ if(i>0) ss << sep; ss << v[i]; } return ss.str(); }

  inline string operator * (string s, int k){ string ret; while(k){ if(k&1) ret += s; s += s; k >>= 1; } return ret; }
}
constexpr long long mod = 9_ten + 7;


template<class T=int>
class Segment_Tree_Lazy{
private:
  //default values are set in the constractor
  const T default_value;    //default (NULL) node value
  const T default_lazy_value; //default lazy value

  struct node{
    T value; T lazy_value;
    bool lazy; int lb; int ub;
    node* par; node* lch; node* rch;

    node(T default_value, T default_lazy_value) : 
      value(default_value),
      lazy_value(default_lazy_value),
      lazy(false),
      lb(0),ub(0),      //this node covers [lb, rb)
      par(NULL),lch(NULL),rch(NULL){
    }
  };

  T evaluate(T left_val, T right_val){  //evaluate node value
    return left_val + right_val;  //sum
    //return max(left_val, right_val);  //max
  }

  T evaluate_node_and_lazy(T node_val, T lazy_val){ //evaluate node value and lazy value
    return node_val + lazy_val; //sum
    //return max(node_val, lazy_val); //max
    //return lazy_val;  //fill
  }
  T evaluate_lazy_and_lazy(T old_val, T new_val){ 
    return old_val + new_val; //sum
    //return new_val;   //fill
  }

  void new_lazy(node* t, T add_value){  
    if(t==NULL) return;
    if(t->lazy){
      t->lazy_value = evaluate_lazy_and_lazy(t->lazy_value, add_value);
    }else{
      t->lazy_value = add_value;
    }
    t->lazy = true;
  }

  T lazy_to_value(node* t){
    if(t->lazy) return t->lazy_value * (t->ub - t->lb);
    return default_lazy_value;
  }

  T get_value(node* t){
    if(t==NULL) return default_value;
    return evaluate_node_and_lazy(t->value , lazy_to_value(t));
  }

  vector<node> v;
  node* root;

  int size;

  node* constract(node* t, int pos, int lb, int ub){
    if(pos == 0){
      t->par = t;
    }
    t->lb = lb;
    t->ub = ub;

    if(pos<size-1){
      t->lch = constract(&v[(pos<<1) + 1], (pos<<1) + 1, lb, (ub+lb)>>1);
      t->lch->par = t;

      t->rch = constract(&v[(pos<<1) + 2], (pos<<1) + 2, (ub+lb)>>1, ub);
      t->rch->par = t;
    }

    return t;
  }

  void initialize(int size_){
    size = 1;
    while(size < size_) size <<= 1;

    v = vector<node>(size<<1, node(default_value, default_lazy_value));
    root = constract(&v[0], 0, 0, size);
  }

  //propagate lazy value of node t to its children
  void push(node* t){
    if(t==NULL) return;
    if(t->lazy){
      if(t->lch != NULL){ //has child
        new_lazy(t->lch, t->lazy_value);
        new_lazy(t->rch, t->lazy_value);

        t->value = evaluate( get_value(t->lch), get_value(t->rch) );
      }else{
        t->value = get_value(t);
      }

      t->lazy_value = default_lazy_value;
      t->lazy = false;
    }
  }


  void range_add(int left, int right, T add_value, node* t){
    if(t==NULL) return;
    if(t->ub <= left || right <= t->lb){
      return;
    }

    push(t);
    if(left <= t->lb && t->ub <= right){
      new_lazy(t, add_value);
      return;
    }

    //partialy covered
    range_add(left, right, add_value, t->lch);
    range_add(left, right, add_value, t->rch);
    
    t->value = evaluate( get_value(t->lch), get_value(t->rch) );
  }

  //get value of [left,right)
  T get_range_value(int left, int right, node* t){
    if(t==NULL) return default_value;
    if(t->ub <= left || right <= t->lb){
      return default_value;
    }

    push(t);
    if(left <= t->lb && t->ub <= right){
      return get_value(t);
    }

    T L = get_range_value(left, right, t->lch);
    T R = get_range_value(left, right, t->rch);

    return evaluate(L, R);
  }

  void lazy_update(node* t){
    if(t->par != root){
      lazy_update(t->par);
    }
    push(t);
  }

public:
  //default node value must be set
  // sum : 0
  // max : MIN_INF
  // min : MAX_INF
  // gcd : 1
  Segment_Tree_Lazy(int size_, T default_value__ = 0, T default_lazy_value__ = 0) : 
    default_value(default_value__), default_lazy_value(default_lazy_value__){
    initialize(size_);
  }


  //node[pos] <= new_value
  void update(int pos, T new_value){
    node* t = &v[pos + size-1];
    
    lazy_update(t);

    t->value = new_value;

    while(t != root){
      t = t->par;
      t->value = evaluate( get_value(t->lch), get_value(t->rch) );
    }
  }

  //[l,r) += add_value
  void range_add(int left, int right, T add_value){
    range_add(left, right, add_value, root);
  }

  //get value [left,right)
  T get_range_value(int left, int right){
    return get_range_value(left, right, root);
  }

  void dbg(){
    for(int i=0; i<v.size(); i++){
      cerr << get_value(&v[i]) << " ";
    }
    cerr << endl;
  }
};

int main(){
  int n,m;
  cin >> n,m;

  vector<int> a(n);
  cin >> a;
  for(auto& x : a){
    x--;
  }

  vector<int> d(n);
  for(int i=1; i<n; i++){
    d[i] = a[i-1] < a[i] ? a[i] - a[i-1] : a[i]+m - a[i-1];
  }

  long long unko = accumulate(d.begin(), d.end(), 0LL);

  Segment_Tree_Lazy<long long> seg(m*3);
  for(int i=1; i<n; i++){
    int l = a[i-1];
    int r = a[i];
    if( l>r ) r += m;

    if( r-l < 2 ) continue;
    seg.range_add( l+2, r+1, 1 );
    seg.range_add( r+1, r+2, -(r+1-(l+2)) );

    l += m;
    r += m;

    seg.range_add( l+2, r+1, 1 );
    seg.range_add( r+1, r+2, -(r+1-(l+2)) );
  }

  long long tmp = 0;
  long long mx = 0;
  for(int i=0; i<m*3; i++){
    auto dec = seg.get_range_value(i, i+1);
    tmp += dec;
    mx = max(mx, tmp);
  }

  println(unko-mx);

  return 0;
}


Measured memory_bytes: 58368