#include <bits/stdc++.h>
using namespace std;
const long long maxn = 3e5 + 10;
const long long mod = 1e9 + 7;
const long long INF = 1e18;
long long n, m;
long long a[maxn];
bool ok(long long k) {
  if (k < 0) return 0;
  long long maxx = 0;
  for (long long i = 1; i <= n; i++) {
    long long x = (maxx - a[i] + m) % m;
    if (x <= k) continue;
    if (a[i] < maxx) return 0;
    maxx = max(maxx, a[i]);
  }
  return 1;
}
int main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  long long hi = m, lo = -1;
  while (hi - lo > 1) {
    long long tm = (hi + lo) / 2;
    if (ok(tm))
      hi = tm;
    else
      lo = tm;
  }
  cout << hi;
}
