#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long inf = 1e15;
const long long N = 3e5 + 10;
long long n, m, a[N];
bool chk(long long x) {
  long long prev = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] + x >= m and (a[i] + x) % m >= prev) continue;
    if ((a[i] + x) < prev) return 0;
    prev = max(a[i], prev);
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long lo = 0, hi = m - 1;
  while ((hi - lo) > 1) {
    long long mid = (hi + lo) / 2;
    if (chk(mid))
      hi = mid;
    else
      lo = mid;
  }
  if (chk(lo))
    cout << lo;
  else
    cout << hi;
  return 0;
}
