#include <bits/stdc++.h>
using namespace std;
int a[300005];
int main() {
  int n, m, l = 0, r = 300005, ans = -1;
  cin >> n >> m;
  for (int i = (1); i <= (n); i++) scanf("%d", &a[i]);
  while (l <= r) {
    int mid = (l + r) >> 1, pre = 0, flag = 1;
    for (int i = (1); i <= (n); i++) {
      int L = a[i], R = a[i] + mid;
      if (R < m) {
        if (R < pre) {
          flag = 0;
          break;
        }
        if (L > pre) pre = L;
      } else {
        if (R % m < pre) {
          if (L > pre) pre = L;
        }
      }
    }
    if (flag)
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%d\n", ans);
}
