#include <bits/stdc++.h>
using namespace std;
int n, m, a[300005];
bool check(int v) {
  int vnow = 0;
  for (int i = (int)(1); i <= (int)(n); i++) {
    int vl = a[i], vr = a[i] + v;
    for (; vnow < m && (vnow < vl || vnow > vr) &&
           (vnow + m < vl || vnow + m > vr);
         ++vnow)
      ;
    if (vnow >= m) return 0;
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = (int)(1); i <= (int)(n); i++) scanf("%d", &a[i]);
  int l = 0, r = m, ans;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
}
