#include <bits/stdc++.h>
using namespace std;
int arr[300001];
int n, m;
bool check(int x) {
  int prev = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] < prev && (prev - arr[i]) > x)
      return false;
    else if (arr[i] >= prev && (m - arr[i] + prev) > x)
      prev = arr[i];
  }
  return true;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int st = 0, en = m - 1, ans = m - 1;
  while (st <= en) {
    int mid = (st + en) / 2;
    if (check(mid))
      ans = mid, en = mid - 1;
    else
      st = mid + 1;
  }
  cout << ans << endl;
}
