#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  int a[300005];
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  int Max = m, Min = -1;
  while (Max > Min + 1) {
    int mid = (Max + Min) / 2;
    int last = 0;
    int ok = 1;
    for (int i = 0; i < n; i++) {
      if (a[i] + mid >= m + last) {
        continue;
      } else {
        if (a[i] + mid < last) {
          ok = 0;
          break;
        } else {
          last = max(a[i], last);
        }
      }
    }
    if (!ok) {
      Min = mid;
    } else {
      Max = mid;
    }
  }
  printf("%d\n", Max);
}
