#include <bits/stdc++.h>
int v[300009];
inline bool check(int n, int k, int m) {
  int x = 0;
  for (int i = 0; i < n; i++) {
    int y = v[i] + k;
    if (y >= m) {
      y -= m;
      if (x > y) x = std::max(x, v[i]);
    } else {
      if (x > y) return 0;
      x = std::max(x, v[i]);
    }
  }
  return 1;
}
int main() {
  int n, m;
  fscanf(stdin, "%d%d", &n, &m);
  for (int i = 0; i < n; i++) fscanf(stdin, "%d", &v[i]);
  int rez = -1;
  for (int pas = 1 << 18; pas; pas >>= 1)
    if (rez + pas < m && check(n, rez + pas, m) == 0) rez += pas;
  fprintf(stdout, "%d\n", rez + 1);
  fclose(stdin);
  fclose(stdout);
  return 0;
}
