#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
long long a[N];
long long n, m;
bool check(long long x) {
  vector<long long> b(n);
  for (long long i = 0; i < n; i++) {
    b[i] = a[i];
  }
  if (b[0] + x >= m) {
    b[0] = 0;
  }
  for (long long i = 1; i < n; i++) {
    if (b[i] + x < m) {
      if (b[i - 1] > b[i] + x) {
        return 0;
      } else {
        if (b[i] >= b[i - 1]) {
          b[i] = b[i];
        } else {
          b[i] = b[i - 1];
        }
      }
    } else {
      if (b[i - 1] <= (b[i] + x) % m) {
        b[i] = b[i - 1];
      } else if (b[i - 1] > (b[i] + x) % m) {
        if (b[i - 1] < b[i]) {
          b[i] = b[i];
        } else {
          b[i] = b[i - 1];
        }
      }
    }
  }
  return 1;
}
signed main() {
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long L = 0, R = 1e12;
  long long ans = -1;
  while (L <= R) {
    long long m = (L + R) / 2;
    if (check(m)) {
      ans = m;
      R = m - 1;
    } else {
      L = m + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
