#include <bits/stdc++.h>
using namespace std;
const long long oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n);
  for (long long i = (0); i < (n); i++) cin >> a[i];
  auto ok = [&](long long k) {
    long long bmin = 0;
    for (long long x : a) {
      long long dist = bmin - x;
      if (dist < 0) dist += m;
      if (dist <= k) continue;
      if (x < bmin) return false;
      bmin = x;
    }
    return true;
  };
  long long rmin = -1, rmax = m;
  while (rmax - rmin > 1) {
    long long r = (rmin + rmax) / 2;
    if (ok(r))
      rmax = r;
    else
      rmin = r;
  }
  cout << rmax << endl;
}
