#include <bits/stdc++.h>
using namespace std;
int n, m, a[300005], ans = 1e9, b[300005], s = 0, e = 300000;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  while (s <= e) {
    int mid = (s + e) / 2, flag = 0;
    for (int i = 1; i <= n; i++) b[i] = a[i];
    for (int i = 1; i <= n; i++) {
      if (b[i] == b[i - 1])
        ;
      if (b[i] < b[i - 1]) {
        if (b[i] + mid < b[i - 1]) {
          s = mid + 1;
          flag = 1;
          break;
        } else
          b[i] = b[i - 1];
      }
      if (b[i] > b[i - 1]) {
        if (b[i] + mid >= m && (b[i] + mid) % m >= b[i - 1])
          b[i] = b[i - 1];
        else
          continue;
      }
    }
    if (!flag) {
      ans = mid, e = mid - 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
