#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007LL;
long long large = 2000000000000000000LL;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n, 0);
  for (int i = 0; i < n; i++) cin >> a[i];
  int low = 0;
  int high = m - 1;
  int ans = m - 1;
  while (low <= high) {
    int mid = (low + high) / 2;
    int pre = 0;
    bool can = true;
    for (int i = 0; i < n; i++) {
      if (a[i] < pre) {
        if (a[i] + mid < pre) {
          can = false;
          break;
        }
      } else if (a[i] + mid < pre + m) {
        pre = a[i];
      }
    }
    if (can) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
