#include <bits/stdc++.h>
int n, m, a[300010], b[300010];
bool ok(int mid) {
  for (int i = 1; i <= n; ++i) b[i] = a[i];
  b[0] = 0;
  for (int i = 1; i <= n; ++i) {
    int prev = b[i - 1];
    if (prev > b[i]) {
      if (prev - b[i] > mid)
        return 0;
      else
        b[i] = prev;
    } else if (prev < b[i]) {
      int howMuch = m - b[i] + prev;
      if (howMuch <= mid) b[i] = prev;
    }
  }
  return 1;
}
int main() {
  std::cin >> n >> m;
  for (int i = 1; i <= n; ++i) std::cin >> a[i];
  int low = 0, high = m;
  int ans = -1;
  while (low <= high) {
    int mid = low + (high - low) / 2;
    if (ok(mid)) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  std::cout << ans;
}
