#include <bits/stdc++.h>
using namespace std;
template <class X, class Y>
bool minimize(X &x, const Y &y) {
  X eps = 1e-9;
  if (x > y + eps) {
    x = y;
    return true;
  } else
    return false;
}
template <class X, class Y>
bool maximize(X &x, const Y &y) {
  X eps = 1e-9;
  if (x + eps < y) {
    x = y;
    return true;
  } else
    return false;
}
template <class T>
T Abs(const T &x) {
  return (x < 0 ? -x : x);
}
int a[300300], n, mod;
int value(int x, int step, int last) {
  if (x + step < mod) {
    return last > x + step ? -1 : max(x, last);
  }
  return last <= (x + step) % mod ? last : max(last, x);
}
bool ok(int t) {
  int last = 0;
  for (int i = (1), _b = (n); i <= _b; i++) {
    last = value(a[i], t, last);
    if (last < 0) return false;
  }
  return true;
}
int process(void) {
  scanf("%d%d", &n, &mod);
  for (int i = (1), _b = (n); i <= _b; i++) scanf("%d", &a[i]);
  int L = 0, R = 300300;
  while (true) {
    if (L == R) return L;
    if (R - L == 1) return ok(L) ? L : R;
    int M = (L + R) >> 1;
    if (ok(M))
      R = M;
    else
      L = M + 1;
  }
}
int main(void) {
  printf("%d\n", process());
  return 0;
}
