#include <bits/stdc++.h>
using namespace std;
int a[300000], m, n;
inline bool check(int shift) {
  int last = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] > last) {
      if (last + m - a[i] > shift) last = a[i];
    } else if (a[i] < last)
      if (last - a[i] > shift) return false;
  }
  return true;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i) scanf("%d", a + i);
  int l = 0, r = m - 1;
  while (l < r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return 0;
}
