#include <bits/stdc++.h>
using namespace std;
int N, M;
int arr[300001];
int t[300001];
bool f(int m) {
  for (int i = 1; i <= N; i++) {
    if (arr[i] + m >= t[i - 1] + M) {
      t[i] = t[i - 1];
    } else {
      if (t[i - 1] <= arr[i]) {
        t[i] = arr[i];
      } else {
        if (t[i - 1] > arr[i] + m)
          return false;
        else
          t[i] = t[i - 1];
      }
    }
  }
  return true;
}
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; i++) {
    scanf("%d", &arr[i]);
  }
  int l = 0, r = M, answ = -1;
  while (l <= r) {
    int m = (l + r) >> 1;
    if (f(m)) {
      r = m - 1;
      answ = m;
    } else
      l = m + 1;
  }
  printf("%d", answ);
}
