#include <bits/stdc++.h>
using namespace std;
const int MXX = 3e5 + 5;
int n, m;
int a[MXX], ans[MXX];
bool ok(int x) {
  int mx = m - 1;
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] <= mx) {
      if (a[i] + x >= mx) {
        ans[i] = mx;
      } else {
        ans[i] = a[i] + x;
      }
    } else {
      if (a[i] + x >= mx + m)
        ans[i] = mx;
      else
        ans[i] = (a[i] + x) % m;
    }
    mx = ans[i];
  }
  for (int i = 0; i < n - 1; i++) {
    if (ans[i] > ans[i + 1]) {
      return false;
    }
  }
  return true;
}
bool ok2() {
  for (int i = 0; i < n - 1; i++) {
    if (a[i] > a[i + 1]) {
      return false;
    }
  }
  return true;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if (ok2()) {
    return cout << 0 << endl, 0;
  }
  int lo = 0, hi = m - 1, mid, ans = -1;
  while (lo <= hi) {
    mid = (lo + hi) / 2;
    if (ok(mid)) {
      ans = mid;
      hi = mid - 1;
    } else {
      lo = mid + 1;
    }
  }
  cout << ans << endl;
  return 0;
}
