#include <bits/stdc++.h>
using namespace std;
const long long maxl = 3e5 + 7;
const long long inf = 1e5;
long long nana[maxl];
long long n, m;
bool check(long long x) {
  long long mx = 0;
  for (long long i = 0; i < n; i++) {
    if (nana[i] + x < mx)
      return false;
    else {
      if (nana[i] + x - m < mx) mx = max(nana[i], mx);
    }
  }
  return true;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n; i++) cin >> nana[i];
  long long l = 0, r = m;
  while (l <= r) {
    long long mid = (l + r) / 2;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  cout << l;
}
