#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> arr;
bool myf(int xx) {
  vector<int> tmp(n);
  for (int i = 0; i < n; i++) tmp[i] = arr[i];
  if (tmp[0] + xx >= m) tmp[0] = 0;
  tmp[0] = min(tmp[0], (tmp[0] + xx) % m);
  for (int i = 1; i < n; i++) {
    int tt = tmp[i];
    if (tmp[i] + xx >= m) {
      if (tmp[i - 1] >= tmp[i])
        tt = tmp[i - 1];
      else {
        if (tmp[i] + xx - m >= tmp[i - 1]) tt = tmp[i - 1];
      }
    } else {
      if (tmp[i - 1] > tmp[i] + xx) return false;
      if (tmp[i - 1] >= tmp[i]) tt = tmp[i - 1];
    }
    tmp[i] = tt;
  }
  return true;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  arr.resize(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  int l = 0, r = m;
  int ans = r;
  while (l <= r) {
    int md = (l + r) / 2;
    if (myf(md)) {
      ans = min(ans, md);
      r = md - 1;
    } else
      l = md + 1;
  }
  cout << ans;
  return 0;
}
