#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  cin >> n >> m;
  int a[n + 5], h = 0;
  int l = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  h = m;
  long long ans = m;
  while (l <= h) {
    long long mid = (l + h) / 2;
    long long cnt = 0;
    for (long long i = 0; i < n; i++) {
      if (cnt < a[i]) {
        if ((m - (a[i] - cnt)) > mid) {
          cnt = a[i];
        }
      } else {
        if ((cnt - a[i]) > mid) {
          l = mid + 1;
          break;
        }
      }
    }
    if (l != mid + 1) {
      ans = min(ans, mid);
      h = mid - 1;
    }
  }
  cout << ans;
  return 0;
}
