#include <bits/stdc++.h>
using namespace std;
int a[300005], b[300005];
int n, m;
bool Check(int x) {
  b[n + 1] = m - 1;
  for (int i = n; i; i--) {
    if (a[i] > b[i + 1]) {
      if (a[i] + x >= m)
        b[i] = min(b[i + 1], (a[i] + x) % m);
      else
        return 0;
    } else
      b[i] = min(b[i + 1], a[i] + x);
    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;
  while (l <= r) {
    int mid = l + r >> 1;
    if (Check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", l);
}
