#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, m;
  cin >> n >> m;
  int ar[n + 3];
  for (int i = 1; i <= n; i++) cin >> ar[i];
  ar[0] = 0;
  ar[n + 1] = m - 1;
  int low = 0, high = m - 1;
  while (low < high) {
    int mid = (low + high) / 2;
    bool bisa = 1;
    int sim[n + 3];
    for (int i = 0; i <= n; i++) sim[i] = ar[i];
    for (int i = 1; i <= n; i++) {
      if (sim[i - 1] - sim[i] > mid) {
        bisa = 0;
        break;
      }
      if (sim[i] <= sim[i - 1])
        sim[i] = sim[i - 1];
      else {
        int tm = (sim[i] + mid) % m;
        if (tm >= sim[i - 1] && sim[i] > tm) sim[i] = sim[i - 1];
      }
    }
    if (bisa)
      high = mid;
    else
      low = mid + 1;
  }
  cout << high << '\n';
  return 0;
}
