#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 3e5;
const long long INF = 1e9;
long long n, m;
bool ok(long long k, vector<long long> a) {
  reverse(a.begin() + 1, a.end());
  a[0] = INF;
  for (int i = 1; i <= n; ++i) {
    if (a[i] <= a[i - 1]) {
      a[i] = min({a[i - 1], a[i] + k});
      if (a[i] >= m) {
        a[i] = m - 1;
      }
    } else {
      if ((a[i] + k) >= m) {
        a[i] = min((a[i] + k) % m, a[i - 1]);
      } else {
        a[i] = min((a[i] + k) % m, a[i]);
      }
    }
    long long tmp = a[i];
    if (a[i - 1] < a[i]) {
      return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  cin >> n >> m;
  vector<long long> a(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  long long l = 0, r = m;
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (ok(mid, a)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  if (ok(l, a)) {
    cout << l << '\n';
  } else {
    cout << r << '\n';
  }
  return 0;
}
