#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> a;
bool ok(int X) {
  int prev = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] >= prev) {
      if (X < m - a[i] + prev) {
        prev = a[i];
      }
    } else {
      if (a[i] + X < prev) {
        return false;
      }
    }
  }
  return true;
}
int main() {
  cin >> n >> m;
  a = vector<int>(n);
  for (int &x : a) {
    cin >> x;
  }
  int l = 0;
  int r = m - 1;
  while (l < r) {
    int m = (l + r) / 2;
    if (ok(m)) {
      r = m;
    } else {
      l = m + 1;
    }
  }
  if (ok(l)) {
    cout << l << endl;
    return 0;
  } else {
    cout << r << endl;
  }
  return 0;
}
