#include <bits/stdc++.h>
using namespace std;
const int n_size = 3e5 + 5;
int n, m, t[n_size], s[n_size];
bool check(int k) {
  for (int i = 1; i <= n; i++) {
    t[i] = s[i];
    if (t[i] >= t[i - 1]) {
      if (t[i] + k >= m) {
        if ((t[i] + k) % m >= t[i - 1])
          t[i] = t[i - 1];
        else
          t[i] = t[i];
      } else {
        t[i] = t[i];
      }
    } else {
      if (t[i] + k >= t[i - 1])
        t[i] = t[i - 1];
      else
        return 0;
    }
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &s[i]);
  }
  int l = 0, r = m;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  printf("%d", l);
  return 0;
}
