#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
long long n, m, M;
long long a[((long long)(2e6 + 500))];
long long dp[((long long)(2e6 + 500))][2];
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long l = 0, r = 2 * m + 1;
  while (l < r) {
    long long M = l + r >> 1;
    long long mn = 0;
    long long e = 1;
    for (int i = 0; i < n; i++) {
      long long k = -1;
      if (a[i] >= mn) {
        k = a[i];
        if (a[i] + M >= m + mn) k = mn;
      } else {
        if (a[i] + M < mn)
          e = 0;
        else
          k = mn;
      }
      mn = k;
    }
    if (e)
      r = M;
    else
      l = M + 1;
  }
  cout << l;
}
