#include <bits/stdc++.h>
using namespace std;
long long mod = (long long)1e9 + 7;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
bool solve(int mid, int a[], int n, int m) {
  int b[n];
  b[0] = a[0];
  if (m - a[0] <= mid) b[0] = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] >= b[i - 1]) {
      b[i] = a[i];
      if (m - a[i] + b[i - 1] <= mid) {
        b[i] = b[i - 1];
      }
    } else {
      if (b[i - 1] - a[i] <= mid) {
        b[i] = b[i - 1];
      } else
        return false;
    }
  }
  return true;
}
int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int lw = 0, hi = m;
  while (lw < hi) {
    int mid = (lw + hi) / 2;
    if (solve(mid, a, n, m)) {
      hi = mid;
    } else {
      lw = mid + 1;
    }
  }
  cout << lw << endl;
}
