#include <bits/stdc++.h>
using namespace std;
long long res, n, k, d = 0, f, s, moy, mx;
long long t[300003];
bool b = true;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (long long i = 0; i < n; i++) cin >> t[i];
  reverse(t, t + n);
  f = k - 1;
  while (d <= f) {
    b = true;
    moy = (d + f) / 2;
    mx = k - 1;
    for (long long i = 0; i < n; i++) {
      if (t[i] <= mx)
        mx = min(mx, t[i] + moy);
      else if ((t[i] + moy) >= k)
        mx = min(mx, t[i] + moy - k);
      else {
        b = false;
        break;
      }
    }
    if (b) {
      res = moy;
      f = moy - 1;
    } else
      d = moy + 1;
  }
  cout << res;
  return 0;
}
