#include <bits/stdc++.h>
using namespace std;
const int maxn = 300050;
inline void read_(int &s) {
  s = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    s = (s << 1) + (s << 3) + c - '0';
    c = getchar();
  }
}
int n, m, l, r, mid, a[maxn], b[maxn];
bool d = 0;
inline bool check(int k) {
  for (register int i = 1; i <= n; i++) b[i] = a[i];
  for (register int i = 1; i <= n; i++) {
    if (!b[i - 1]) {
      if ((b[i] + k) >= m) b[i] = 0;
    } else {
      if (b[i] < b[i - 1] && b[i] + k >= b[i - 1])
        b[i] = b[i - 1];
      else if (b[i] > b[i - 1] && (b[i] + k) >= (m + b[i - 1]))
        b[i] = b[i - 1];
      else if (b[i] < b[i - 1])
        return 0;
    }
  }
  return 1;
}
int main() {
  read_(n);
  read_(m);
  for (register int i = 1; i <= n; i++) {
    read_(a[i]);
    if (a[i] < a[i - 1]) d = 1;
  }
  if (!d) {
    printf("0");
    exit(0);
  }
  l = 1;
  r = m;
  while (l < r) {
    mid = l + r >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  printf("%d", r);
  return 0;
}
