#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int ans = 0;
  int s = 0, e = m - 1;
  while (s <= e) {
    int mid = (s + e) / 2;
    int temp[n];
    for (int i = 0; i < n; i++) {
      temp[i] = a[i];
    }
    if (a[0] + mid >= m) {
      temp[0] = 0;
    }
    bool d = true;
    for (int i = 1; i < n; i++) {
      int sub = (temp[i] + mid) % m;
      if (temp[i] > temp[i - 1]) {
        if (sub < temp[i] && sub >= temp[i - 1]) {
          temp[i] = temp[i - 1];
        }
      }
      if (temp[i - 1] > temp[i]) {
        if (temp[i - 1] - temp[i] > mid) {
          d = false;
          s = mid + 1;
          break;
        } else {
          temp[i] = temp[i - 1];
        }
      }
    }
    if (d) {
      ans = mid;
      e = mid - 1;
    }
  }
  cout << ans;
}
