#include <bits/stdc++.h>
template <typename T>
inline void read(T& x) {
  T res = 0, flag = 1;
  char ch;
  ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') flag = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    res = res * 10 + ch - 48;
    ch = getchar();
  }
  x = res * flag;
}
const int N = 300000 + 1;
int n, m, a[N], f[N];
inline bool check(int x) {
  for (int i = 1; i <= n; ++i) {
    f[i] = a[i];
    if (f[i] >= f[i - 1]) {
      if (f[i] + x >= m)
        if ((f[i] + x) % m >= f[i - 1]) f[i] = f[i - 1];
    } else {
      if (f[i] + x >= f[i - 1])
        f[i] = f[i - 1];
      else
        return 0;
    }
  }
  return 1;
}
int main() {
  read(n), read(m);
  for (int i = 1; i <= n; ++i) read(a[i]);
  int l = 0, r = m;
  while (l < r) {
    int mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
}
