#include <bits/stdc++.h>
const int N = 300010;
using namespace std;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ('0' > ch || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  return x * f;
}
int n, m;
int a[N];
bool check(int lp) {
  int p = 0;
  for (register int i = 1; i <= n; i++) {
    if (a[i] <= p) {
      if (p - a[i] <= lp)
        continue;
      else
        return 0;
    } else {
      if (a[i] + lp >= m && (a[i] + lp) % m >= p)
        continue;
      else
        p = a[i];
    }
  }
  return 1;
}
int main() {
  n = read(), m = read();
  for (register int i = 1; i <= n; i++) a[i] = read();
  int l = 0, r = m, ans = m;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
}
