#include <bits/stdc++.h>
using namespace std;
const int maxn = 201010;
int n, m, k, len = 0, x, y, a[301010], b[301010];
long long rd() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void init() {
  n = rd();
  m = rd();
  for (int i = 1; i <= n; i++) a[i] = rd();
}
bool check(int x) {
  for (int i = 1; i <= n; i++) {
    if (b[i] < b[i - 1]) {
      if (b[i] + x >= b[i - 1]) b[i] = b[i - 1];
    } else {
      if ((b[i] + x) % m >= b[i - 1] && (b[i] + x) >= m) b[i] = b[i - 1];
    }
    if (b[i] < b[i - 1]) return 1;
  }
  return 0;
}
void work() {
  int ans = 0, l = 0, r = m;
  while (l <= r) {
    for (int i = 1; i <= n; i++) b[i] = a[i];
    int mid = (l + r) >> 1;
    if (check(mid)) {
      l = mid + 1;
    } else {
      r = mid - 1;
      ans = mid;
    }
  }
  printf("%d\n", ans);
}
int main() {
  init();
  work();
}
