#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  int l = 0, r = 3e6;
  while (l != r) {
    int mid = (l + r) >> 1;
    bool ok = true;
    int prev = a[0];
    if (m - prev <= mid) prev = 0;
    for (int i = 1; i < n; i++) {
      if (a[i] == prev) continue;
      if (a[i] < prev) {
        int dif = prev - a[i];
        if (dif > mid) ok = false;
      } else {
        int dif = prev + m - a[i];
        if (dif > mid) prev = a[i];
      }
    }
    if (ok)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l << endl;
  return (0);
}
