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