#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
int a[maxn];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l = -1, r = m;
  while (l < r - 1) {
    int mid = (l + r) / 2;
    int prev = 0;
    bool bad = false;
    for (int i = 0; i < n; i++) {
      int lf = a[i], rf = a[i] + mid;
      if ((lf <= prev && prev <= rf) || (lf <= prev + m && prev + m <= rf)) {
        continue;
      }
      if (lf < prev) {
        bad = true;
        break;
      } else {
        prev = lf;
      }
    }
    if (bad)
      l = mid;
    else
      r = mid;
  }
  cout << r << endl;
}
