#include <bits/stdc++.h>
using namespace std;
int a[300005];
int n, m;
bool isOk(int x) {
  int pre = 0, lf, rf;
  for (int i = 1; i <= n; i++) {
    lf = a[i], rf = a[i] + x;
    if ((lf <= pre && pre <= rf) || (lf <= pre + m && pre + m <= rf)) continue;
    if (lf < pre)
      return false;
    else
      pre = lf;
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int l = 0, r = m, mid;
  int ans = 0x7fffffff;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (isOk(mid)) {
      r = mid - 1;
      ans = min(ans, mid);
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
