#include <bits/stdc++.h>
using namespace std;
int n, m, l, k, r, a[300010];
bool check(int k) {
  int mini = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= mini) {
      if (mini - a[i] <= k) continue;
      return false;
    }
    if ((m - a[i]) + mini <= k) continue;
    mini = a[i];
  }
  return true;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; ++i) scanf("%i", &a[i]);
  for (l = -1, r = m; l + 1 < r;) {
    k = (l + r) / 2;
    if (check(k))
      r = k;
    else
      l = k;
  }
  cout << r;
  return 0;
}
