#include <bits/stdc++.h>
using namespace std;
inline void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
long long int n, m;
bool check(long long int a[], long long int mid) {
  long long int bf = 0;
  for (long long int i = 1; i <= n; i++) {
    if (bf <= a[i]) {
      long long int t = m - (a[i] - bf);
      if (t > mid) bf = a[i];
    } else {
      long long int t = (bf - a[i]);
      if (t > mid) return false;
    }
  }
  return true;
}
int main() {
  fast();
  cin >> n >> m;
  long long int a[n + 1];
  a[0] = 0;
  for (long long int i = 1; i <= n; i++) cin >> a[i];
  long long int lo = 0, hi = m;
  while (lo != hi) {
    long long int mid = (lo + hi) / 2;
    if (check(a, mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  long long int ans = m;
  if (check(a, hi)) {
    ans = min(hi, ans);
  }
  if (check(a, lo)) ans = min(lo, ans);
  cout << ans << endl;
}
