#include <bits/stdc++.h>
using namespace std;
const int _ = 5e5 + 7;
int n, m, a[_], b[_];
bool check(int mid) {
  memset(b, -1, sizeof(b));
  b[0] = 0;
  for (int i = 1; i <= n; ++i) {
    int L = a[i], R = (a[i] + mid) % m;
    if (L <= R) {
      if (b[i - 1] <= R)
        b[i] = max(b[i - 1], L);
      else
        return 0;
    } else {
      if (R < b[i - 1] && b[i - 1] < L)
        b[i] = L;
      else
        b[i] = b[i - 1];
    }
    if (b[i] < b[i - 1]) return 0;
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
  int l = 0, r = m - 1, ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
  return 0;
}
