#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
int n, m, a[maxn], b[maxn];
bool chk(long long x) {
  for (int i = 1; i <= n; i++) {
    b[i] = a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (b[i - 1] > b[i]) {
      if (b[i - 1] - b[i] > x) return 0;
      b[i] = b[i - 1];
    } else if (m - b[i] + b[i - 1] <= x) {
      b[i] = b[i - 1];
    }
  }
  return 1;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  long long l = 0, r = m, ans;
  while (l <= r) {
    long long mid = (l + r) >> 1;
    chk(mid) ? r = (ans = mid) - 1 : l = mid + 1;
  }
  printf("%lld\n", ans);
  return 0;
}
