#include <bits/stdc++.h>
using namespace ::std;
int ar[300005], n, m;
bool can(int t) {
  int last = 0;
  for (int i = 0; i < n; i++)
    if (ar[i] < last && last - ar[i] > t)
      return 0;
    else if (ar[i] > last && last + m - ar[i] > t)
      last = ar[i];
  return 1;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> ar[i];
  int l = 0, r = m - 1;
  while (l < r) {
    int mid = l + (r - l) / 2;
    if (can(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << '\n';
}
