#include <bits/stdc++.h>
using namespace std;
int a[300000], m, n;
inline void read(register int& n) {
  n = 0;
  register char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  while (c <= '9' && c >= '0') {
    n = (n << 1) + (n << 3) + c - '0';
    c = getchar();
  }
}
inline bool check(int shift) {
  int last = 0;
  for (register 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() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (register int i = 0; i < n; ++i) cin >> a[i];
  int l = -1, r = m - 1;
  while (r - l > 1) {
    int mid = (l + r) >> 1;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r;
  return 0;
}
