#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 3e5;
int a[N];
bool possible(int n, int m, int k) {
  int mx = 0;
  for (int i = 0; i < n; i++) {
    int val = a[i];
    if (mx < val) {
      if (val + k >= m + mx) val = mx;
    } else {
      if (val + k >= mx) val = mx;
    }
    if (val < mx) return false;
    mx = val;
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  int lo = 0, hi = m;
  if (possible(n, m, 0)) hi = 0;
  while (lo + 1 < hi) {
    int mid = (lo + hi) / 2;
    if (possible(n, m, mid)) {
      hi = mid;
    } else {
      lo = mid;
    }
  }
  cout << hi << endl;
  return 0;
}
