#include <bits/stdc++.h>
using namespace std;
int* a = NULL;
int n, m;
bool chk(int, int);
int main() {
  int ll, ul;
  cin >> n >> m;
  a = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  ll = -1;
  ul = m;
  while (ll < ul - 1) {
    if (chk(ll, ul))
      ul = (ll + ul) / 2;
    else
      ll = (ll + ul) / 2;
  }
  cout << ul;
  return 0;
}
bool chk(int x, int y) {
  int prev = 0;
  bool bad = false;
  int mid = (x + y) / 2;
  for (int i = 0; i < n; i++) {
    int t = a[i];
    if (t <= prev && t >= prev - mid || t <= prev + m && t >= prev + m - mid)
      continue;
    if (t > prev)
      prev = t;
    else
      bad = true;
  }
  return !bad;
}
