#include<memory>
using Size = int;
template<typename T> class SegmentTree {
 public:
  SegmentTree(Size SIZE, T INF) : SIZE(SIZE), INF(INF), ROOT(std::make_unique<Node>(0, INF)) {
    ROOT->update = 0;
  }
  void update(Size a, Size b, T v) {update(a, b, v, ROOT, 0, SIZE);}
  void add(Size a, Size b, T v) {add(a, b, v, ROOT, 0, SIZE);}
  T sum(Size a, Size b) {return query(a, b).sum;}
  T min(Size a, Size b) {return query(a, b).min;}
  T max(Size a, Size b) {return query(a, b).max;}
  Size lower_bound(Size a, Size b, T v) {return lower_bound(a, b, v, ROOT, 0, SIZE);}
 private:
  struct Node {
    T sum;
    T min;
    T max;
    T update;
    T add;
    std::unique_ptr<Node> l_child, r_child;
    Node(T v, T INF) : sum(v), min(v), max(v), update(INF), add(INF) {}
  };
  inline void push(const auto& n, Size l, Size r) {
    if(not n->l_child) n->l_child = std::make_unique<Node>(0, INF);
    if(not n->r_child) n->r_child = std::make_unique<Node>(0, INF);
    if(n->update != INF) {
      n->add = INF;
      n->l_child->update = n->r_child->update = n->update;
      n->sum = (r - l) * n->update;
      n->min = n->update;
      n->max = n->update;
      n->update = INF;
    }
    if(n->add != INF) {
      if(n->l_child->add != INF) n->l_child->add += n->add;
      else                       n->l_child->add = n->add;
      if(n->r_child->add != INF) n->r_child->add += n->add;
      else                       n->r_child->add = n->add;
      if(n->l_child->update != INF) n->l_child->update += n->add;
      if(n->r_child->update != INF) n->r_child->update += n->add;
      n->sum += (r - l) * n->add;
      n->min += n->add;
      n->max += n->add;
      n->add = INF;
    }
  }
  inline void fix(const auto& n) {
    n->sum = n->l_child->sum + n->r_child->sum;
    n->min = std::min(n->l_child->min, n->r_child->min);
    n->max = std::max(n->l_child->max, n->r_child->max);
  }
  void update(Size a, Size b, T v, const auto& n, Size l, Size r) {
    push(n, l, r);
    if(b <= l || r <= a) return;
    if(a <= l && r <= b) {
      n->update = v;
      push(n, l, r);
      return;
    }
    auto m = (l + r) / 2;
    update(a, b, v, n->l_child, l, m);
    update(a, b, v, n->r_child, m, r);
    fix(n);
  }
  void add(Size a, Size b, T v, const auto& n, Size l, Size r) {
    push(n, l, r);
    if(b <= l || r <= a) return;
    if(a <= l && r <= b) {
      n->add = v;
      push(n, l, r);
      return;
    }
    auto m = (l + r) / 2;
    add(a, b, v, n->l_child, l, m);
    add(a, b, v, n->r_child, m, r);
    fix(n);
  }
  Node query(Size a, Size b) {
    Node res(0, INF);
    res.min = INF;
    res.max = -INF;
    query(res, a, b, ROOT, 0, SIZE);
    return res;
  }
  void query(auto& res, Size a, Size b, const auto& n, Size l, Size r) {
    push(n, l, r);
    if(b <= l || r <= a) return;
    if(a <= l && r <= b) {
      res.sum += n->sum;
      res.min = std::min(res.min, n->min);
      res.max = std::max(res.max, n->max);
      return;
    }
    auto m = (l + r) / 2;
    query(res, a, b, n->l_child, l, m);
    query(res, a, b, n->r_child, m, r);
  }
  Size lower_bound(Size a, Size b, T v, const auto& n, Size l, Size r) {
    push(n, l, r);
    if(b <= l || r <= a) return SIZE;
    if(n->max < v) return SIZE;
    if(l + 1 == r) return l;
    auto m = (l + r) / 2;
    auto i = lower_bound(a, b, v, n->l_child, l, m);
    if(i != SIZE) return i;
    return lower_bound(a, b, v, n->r_child, m, r);
  }
  const Size SIZE;
  const T INF;
  const std::unique_ptr<Node> ROOT;
};

#include<bits/stdc++.h>
using namespace std;
using I = long long;
int main() {
  int n, m;
  cin >> n >> m;
  vector<I> a(n);
  for(auto& i: a) cin >> i, --i;
  SegmentTree<I> T(2 * m + 2, 1e18);
  I sum = 0;
  for(auto i=1; i<n; ++i) {
    auto l = a[i-1];
    auto h = a[i];
    if(l < h) {
      auto d = h - l;
      sum += d;
      if(l+1 == h) continue;
      T.add(m+l+2, m+h+1, 1);
      T.add(m+h+1, m+h+2, -(d-1));
    } else {
      auto d = h+1 + (m-l-1);
      sum += d;
      if(l==m-1 && h==0) continue;
      T.add(m+l+2, m+m+1, 1);
      T.add(m-(m-l)+2, m+h+1, 1);
      T.add(m+h+1, m+h+2, -(d-1));
    }
  // for(auto i=0; i<2*m; ++i) cout<<" "<<T.sum(i,i+1);cout<<endl;
  }
  for(auto i=1; i<2*m; ++i) {
    auto x = T.sum(i-1, i);
    T.add(i, i+1, x);
  }
  // cout<<sum<<endl;
  // for(auto i=m; i<2*m; ++i) cout<<" "<<T.sum(i,i+1);cout<<endl;
  cout << sum - T.max(m, 2*m) << endl;
}


Measured memory_bytes: 51072