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