#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &x, T y) {
  return x > y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmax(T &x, T y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline void read(T &x) {
  char ch = getchar();
  int f = 1;
  x = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  x *= f;
}
template <typename T, typename... Args>
inline void read(T &x, Args &...args) {
  read(x), read(args...);
}
int n, m, a[300050], ans;
bool solve(int x) {
  int pr = 0;
  for (register int i = 1; i <= n; ++i) {
    if (pr <= a[i]) {
      int _d = m - a[i] + pr;
      if (_d > x) pr = a[i];
    } else {
      int _d = pr - a[i];
      if (_d > x) return 0;
    }
  }
  return 1;
}
int main() {
  read(n, m);
  for (register int i = 1; i <= n; ++i) read(a[i]);
  int l = 0, r = m;
  while (l <= r) {
    if (solve(((l + r) >> 1)))
      ans = ((l + r) >> 1), r = ((l + r) >> 1) - 1;
    else
      l = ((l + r) >> 1) + 1;
  }
  cout << ans << endl;
  return 0;
}
