#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  if (a == 0) {
    return b;
  }
  return gcd(b % a, a);
}
long long int power(long long int x, long long int y) {
  long long int res = 1;
  while (y > 0) {
    if (y % 2 == 1) {
      res = (res * x);
    }
    x = (x * x);
    y = y / 2;
  }
  return res;
}
int main() {
  long long int n, m, i, l, r;
  scanf("%lld", &n);
  scanf("%lld", &m);
  long long int a[n];
  for (i = 0; i < n; i++) scanf("%lld", &a[i]);
  l = 0;
  r = m;
  long long int fl = 0, b, mid;
  while (l < r) {
    mid = (l + r) / 2;
    if (m - a[0] <= mid)
      b = 0;
    else
      b = a[0];
    fl = 0;
    for (i = 1; i < n; i++) {
      if (a[i] > b) {
        if (m - (a[i] - b) > mid) b = a[i];
      } else {
        if (b - a[i] > mid) {
          fl = 1;
          break;
        }
      }
    }
    if (fl)
      l = mid + 1;
    else
      r = mid;
  }
  printf("%lld", l);
  return 0;
}
