#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
inline int add(int a, int b) {
  a += b;
  return a >= 1000000007 ? a - 1000000007 : a;
}
inline int sub(int a, int b) {
  a -= b;
  return a < 0 ? a + 1000000007 : a;
}
inline int mul(int a, int b) { return (long long int)a * b % 1000000007; }
int Set(int N, int pos) { return N = N | (1 << pos); }
int Reset(int N, int pos) { return N = N & ~(1 << pos); }
bool Cheek(int N, int pos) { return (bool)(N & (1 << pos)); }
int n, m, ara[300009];
bool isPossible(int inc) {
  int last = ara[1];
  bool fg = 1;
  if (m - ara[1] <= inc) last = 0;
  for (int i = 2; i <= n && fg; i++) {
    if (ara[i] == last)
      continue;
    else if (ara[i] < last) {
      if (last - ara[i] > inc) fg = 0;
    } else {
      if ((m - ara[i]) + last <= inc)
        last = last;
      else
        last = ara[i];
    }
  }
  return fg;
}
void Solve(int t) {
  int i, j, k;
  scanf("%d %d", &(n), &(m));
  for (i = 1; i <= n; i++) scanf("%d", &(ara[i]));
  int lo = 0, hi = 3e6, mid, res = -1;
  while (lo <= hi) {
    mid = (lo + hi) >> 1;
    if (isPossible(mid)) {
      res = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << res << endl;
}
int main() {
  int t, T;
  T = 1;
  for (__typeof(t) t = 1; t <= T; t++) {
    Solve(t);
  }
  return 0;
}
