#include <bits/stdc++.h>
using LL = long long;
using ULL = unsigned long long;
using namespace std;
long long a[300000];
long long n, m;
bool check(long long mid) {
  long long mn = 0;
  for (long long i = (0); i < (n); i++) {
    if (a[i] <= mn) {
      if (mn - a[i] > mid) return false;
    } else {
      if (m - a[i] + mn > mid) mn = a[i];
    }
  }
  return true;
}
signed main() {
  ios::sync_with_stdio(false), cin.tie(NULL);
  cin >> n >> m;
  for (long long i = (0); i < (n); i++) cin >> a[i];
  long long l = 0, r = m - 1;
  long long ans;
  while (l <= r) {
    long long mid = (l + r) / 2;
    bool ok = check(mid);
    if (ok)
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans << '\n';
  return 0;
}
