#include <bits/stdc++.h>
using namespace std;
long long COSTMAX = 1e16;
int n, m;
int a[300001];
bool decision(int k) {
  int prev = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] + k < prev) return false;
    if (a[i] > prev && a[i] + k < m + prev) prev = a[i];
  }
  return true;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  int lo = 0, hi = m;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    if (decision(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << hi;
}
