#include <bits/stdc++.h>
using namespace std;
const int inf = 3e5 + 9;
int a[inf], temp[inf], n, m;
bool check(int x) {
  for (int i = 0; i <= n; i++) temp[i] = a[i];
  for (int i = 1; i <= n; i++) {
    if (temp[i] <= temp[i - 1]) {
      if (temp[i - 1] - temp[i] > x) return 0;
      temp[i] = temp[i - 1];
    } else {
      if (m - temp[i] + temp[i - 1] <= x) temp[i] = temp[i - 1];
    }
  }
  return 1;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int l = -1, r = m + 1;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << endl;
}
