#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void checkmin(T &a, T b) {
  if (b < a) a = b;
}
template <class T>
inline void checkmax(T &a, T b) {
  if (b > a) a = b;
}
int N, M;
int a[500202], b[500202];
bool isok(int n) {
  int pre = 0;
  for (int i = (0); i < (int)(N); i++) {
    b[i] = a[i];
    if (b[i] == pre)
      continue;
    else if (b[i] < pre) {
      if (b[i] + n >= pre) {
        b[i] = pre;
      } else
        return false;
    } else if (b[i] > pre) {
      if (b[i] + n >= M + pre) {
        b[i] = pre;
      } else {
        pre = b[i];
      }
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &N, &M);
  for (int i = (0); i < (int)(N); i++) scanf("%d", a + i);
  int l = 0, r = M - 1;
  int ret = 1000000001;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (isok(mid)) {
      ret = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d\n", ret);
}
