#include <bits/stdc++.h>
using namespace std;
int n, m, a[300005], b[300005], l, r, mid;
int possible(int x) {
  for (int i = 1; i <= n; i++) b[i] = a[i];
  if (b[1] + x >= m) b[1] = 0;
  for (int i = 2; i <= n; i++) {
    if (b[i] + x < b[i - 1]) return 0;
    if (b[i] >= b[i - 1] && b[i] + x >= m + b[i - 1] ||
        b[i] < b[i - 1] && b[i] + x >= b[i - 1])
      b[i] = b[i - 1];
  }
  return 1;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  l = 0;
  r = 400000;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (possible(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l << endl;
}
