#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10;
int a[maxn], n, m;
bool check(int x) {
  int prev = 0;
  for (int i = 0; i < n; i++) {
    int lf = a[i], rf = a[i] + x;
    if ((lf <= prev && prev <= rf) || (lf <= prev + m && prev + m <= rf))
      continue;
    if (lf < prev)
      return false;
    else {
      prev = lf;
    }
  }
  return true;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  int l = 0, r = m, mid, ans = 0;
  while (r > l) {
    mid = (l + r) / 2;
    if (check(mid))
      r = mid, ans = mid;
    else
      l = mid + 1;
  }
  cout << ans;
}
