#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
const int inf = 0x3f3f3f3f;
int n, x, m;
int a[maxn];
int b[maxn];
bool check(int x) {
  for (int i = 1; i <= n; ++i) b[i] = a[i];
  if (b[1] + x >= m) b[1] = 0;
  for (int i = 2; i <= n; ++i) {
    if (b[i] > b[i - 1]) {
      if (b[i] + x - m >= b[i - 1]) b[i] = b[i - 1];
    } else if (b[i] == b[i - 1])
      continue;
    else if (b[i] < b[i - 1]) {
      if (b[i] + x >= b[i - 1]) b[i] = b[i - 1];
    }
  }
  for (int i = 2; i <= n; ++i) {
    if (b[i] < b[i - 1]) return false;
  }
  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;
  int ans;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
}
