#include <bits/stdc++.h>
#define int long long int
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define RFORE(i,a,b) for(int i=(b);i>=(a);--i)
#define REP(i,n) FOR(i,0,n)
#define ALL(c) (c).begin(),(c).end()
#define RALL(c) (c).rbegin(),(c).rend()
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SZ(c) (int)((c).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define LB(c,x) (int)(lower_bound(ALL(c),x)-(c).begin())
#define UB(c,x) (int)(upper_bound(ALL(c),x)-(c).begin())
#define COUNT(c,x) (UB(c,x)-LB(c,x))
#define UNIQUE(c) SORT(c); (c).erase(unique(ALL(c)),(c).end());
#define COPY(c1,c2) copy(ALL(c1),(c2).begin())
#define EXIST(s,e) (bool)((s).find(e)!=(s).end())
#define PB push_back
#define MP make_pair
#define DEL(v) decltype(v)().swap(v)
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
using namespace std;
template<typename T1, typename T2> using P = pair<T1,T2>;
template<typename T> using V = vector<T>;
struct edge { int from, to; int cost; };
bool operator< (const edge &edge1, const edge &edge2) { return edge1.cost < edge2.cost; };
const int INF = 1e18;

template<typename T> ostream& operator << (ostream& s, const V<T>& v) {
   s << "[";
   for (int i = 0; i < v.size(); i++) { s << v[i]; if (i < v.size() - 1) s << " "; }
   s << "]";
   return s;
}

int m;
int mod(int x) {
   return ((x % m) + m) % m;
}

template<typename T> class BIT
{
   private:
      vector<T> bit;
      int len;
   public:
      BIT(int n) {
         len = n+1;
         bit.resize(len);
      }
      void add(int i, T x) {
         i += 1;
         while (i < len) {
            bit[i] += x;
            i += i & -i;
         }
      }
      int sum(int i) {
         i += 1;
         T s = 0;
         while (i > 0) {
            s += bit[i];
            i -= i & -i;
         }
         return s;
      }
};

signed main()
{
   int n; cin >> n >> m;
   V<int> a(n);
   REP(j, n) {
      cin >> a[j]; a[j]--;
   }

   V<int> dist(n-1);
   REP(j, n-1) {
      dist[j] = mod(a[j+1] - a[j]);
   }
   DUMP(dist)

   V<int> cost(m);
   REP(j, n-1) {
      int d = mod(0 - a[j]);      // distance to the favorite point 0
      if (d == 0 || dist[j] < d) {
         cost[0] += dist[j];
      } else {
         cost[0] += dist[j] - d + 1;
      }
   }
   DUMP(cost[0])

   BIT<int> diff(m+1);
   REP(j, n-1) {
      if (dist[j] == 0) continue;

      if (a[j+1] <= mod(a[j+1]+1)) {
         diff.add(       a[j+1],  dist[j]-1);
         diff.add(mod(a[j+1]+1), -dist[j]+1);
      } else {
         diff.add(       a[j+1],  dist[j]-1);
         diff.add(            m, -dist[j]+1);
         diff.add(            0,  dist[j]-1);
         diff.add(mod(a[j+1]+1), -dist[j]+1);
      }
      if (mod(a[j]+1) <= a[j+1]) {
         diff.add(mod(a[j]+1), -1);
         diff.add(     a[j+1],  1);
      } else {
         diff.add(mod(a[j]+1), -1);
         diff.add(          m,  1);
         diff.add(          0, -1);
         diff.add(     a[j+1],  1);
      }
   }

   REP(i, m-1) {
      cost[i+1] = cost[i] + diff.sum(i);
   }
   SORT(cost);
   cout << cost[0] << endl;

   return 0;
}


Measured memory_bytes: 3328