#include <bits/stdc++.h>
using namespace std;
struct Interval {
  int start, end;
};
bool compare(Interval i1, Interval i2) { return (i1.start < i2.start); }
int main() {
  long long int n, m;
  cin >> n >> m;
  long long int a[n];
  for (long long int i = 0; i < (n); ++i) cin >> a[i];
  long long int l = 0, r = m;
  long long int coun = 0, last = m;
  while (l <= r) {
    long long int mid = l + (r - l) / 2;
    long long int c = 0;
    vector<long long int> v;
    v.assign(a, a + n);
    if ((v[0] + mid) % m < v[0]) v[0] = 0;
    for (int i = 1; i <= (n - 1); ++i) {
      if (v[i] < v[i - 1]) {
        if (v[i - 1] - v[i] <= mid)
          v[i] = v[i - 1];
        else {
          c = 1;
          break;
        }
      } else {
        if (m - v[i] + v[i - 1] <= mid) v[i] = v[i - 1];
      }
    }
    if (mid == last) break;
    if (c == 0)
      r = mid;
    else
      l = mid + 1;
    last = mid;
  }
  cout << l;
  return 0;
}
