#include <bits/stdc++.h>
using namespace std;
int n, m;
int box[300010];
bool check(int times) {
  int mi = 0;
  for (int i = 0; i < n; ++i) {
    if (box[i] + times < m) {
      if (mi > box[i] + times) return false;
      mi = max(mi, box[i]);
    } else {
      if (mi >= box[i])
        continue;
      else if (box[i] + times - m >= mi)
        continue;
      else
        mi = box[i];
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &box[i]);
  }
  int l = 0, r = m, mid, ans = -1;
  while (l <= r) {
    mid = (l + r) / 2;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  printf("%d", ans);
  return 0;
}
