#include <bits/stdc++.h>
using namespace std;
long long n, m;
vector<long long> a;
bool check(long long x) {
  long long mn = 0;
  for (long long i = 0; i < n; i++) {
    if (a[i] <= mn) {
      if (mn - a[i] > x) {
        return false;
      } else {
        continue;
      }
    } else {
      if (a[i] + x - m < mn) {
        mn = a[i];
      } else {
        continue;
      }
    }
  }
  return true;
}
signed main() {
  cin >> n >> m;
  a.resize(n);
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long l = -1;
  long long r = n * m;
  while (r - l > 1) {
    long long mid = (r + l) / 2;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << endl;
  return 0;
}
