#include <bits/stdc++.h>
using namespace std;
const int dim = 300005;
int n, m, a[dim];
int main() {
  int i, ans, d;
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int l = 0, r = dim - 5, mij;
  while (l <= r) {
    mij = (l + r) / 2;
    bool var = true;
    int cur = 0;
    for (i = 1; i <= n; i++) {
      if (a[i] > cur) {
        int d = m - (a[i] - cur);
        if (d > mij) {
          cur = a[i];
        }
      } else {
        d = cur - a[i];
        if (d > mij) {
          var = false;
          break;
        }
      }
    }
    if (var) {
      ans = mij;
      r = mij - 1;
    } else
      l = mij + 1;
  }
  cout << ans;
  return 0;
}
