#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
long long n, m, k, t, a[N], l, r, mid, sum;
inline long long read() {
  char ch;
  long long f = 1;
  while (!isdigit(ch = getchar()))
    if (ch == '-') f = -1;
  long long x = ch - '0';
  while (isdigit(ch = getchar())) x = x * 10 + ch - '0';
  return x * f;
}
bool solve(long long x) {
  long long tmp = 0;
  for (long long i = 1; i <= n; i++)
    if (a[i] < tmp && tmp - a[i] > x)
      return 0;
    else if (a[i] > tmp && m - a[i] + tmp > x)
      tmp = a[i];
  return 1;
}
signed main() {
  n = read(), m = read(), r = m, sum = m;
  for (long long i = 1; i <= n; i++) a[i] = read();
  while (l < r) {
    mid = (l + r) / 2;
    if (solve(mid))
      sum = min(sum, mid), r = mid;
    else
      l = mid + 1;
  }
  printf("%lld\n", sum);
  return 0;
}
