#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int n, m;
vector<int> a;
bool chk(int c) {
  int lst = a[0];
  if (a[0] + c >= m) lst = 0;
  for (int i = 1; i <= (n - 1); ++i) {
    if (a[i] + c >= m + lst) continue;
    if (a[i] < lst && a[i] + c >= lst) continue;
    if (a[i] + c < lst) return false;
    lst = a[i];
  }
  return true;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  cin >> n >> m;
  a.resize(n);
  for (int i = 0; i < (n); ++i) cin >> a[i];
  bool sorted = true;
  for (int i = 0; i < (n); ++i) {
    if (i != 0 && a[i - 1] > a[i]) sorted = false;
  }
  if (sorted) {
    cout << 0 << endl;
    return 0;
  }
  int lo = 0, hi = m, mid = (hi + lo) / 2;
  while (hi - lo > 1) {
    if (chk(mid))
      hi = mid;
    else
      lo = mid;
    mid = (hi + lo) / 2;
  }
  cout << hi << endl;
  return 0;
}
