#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n, m;
vector<long long> a;
bool check(long long cnt) {
  vector<long long> b(n);
  bool res = true;
  for (long long i = 0; i < n; ++i) b[i] = a[i];
  if (m - b[0] <= cnt) b[0] = 0;
  for (long long i = 1; i < n; i++) {
    if (m - b[i] + b[i - 1] <= cnt) {
      b[i] = b[i - 1];
      continue;
    }
    if (b[i] >= b[i - 1]) continue;
    if (b[i - 1] - b[i] <= cnt) {
      b[i] = b[i - 1];
      continue;
    } else {
      res = false;
      break;
    }
  }
  return res;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  a.resize(n);
  for (long long i = 0; i < n; ++i) cin >> a[i];
  long long l = -1;
  long long r = 1e9 + 3;
  while (r - l > 1) {
    long long m = (l + r) / 2;
    if (check(m))
      r = m;
    else
      l = m;
  }
  cout << r << "\n";
  return 0;
}
