#include <bits/stdc++.h>
using namespace std;
const int N = 3e5;
int n, m;
int a[N];
bool check(int d) {
  int last = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= last) {
      if (a[i] + d < last) return false;
    } else {
      if (a[i] + d - m < last) last = a[i];
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) scanf("%d", a + i);
  int l = -1, r = m + 5, d;
  while (r - l > 1) {
    d = (l + r) / 2;
    if (check(d))
      r = d;
    else
      l = d;
  }
  printf("%d", r);
}
