#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[300005];
bool check(int k) {
  int pre = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > pre) {
      if (a[i] + k >= m && (a[i] + k) % m >= pre)
        continue;
      else
        pre = a[i];
    } else if (a[i] < pre) {
      if (a[i] + k < pre) return false;
    }
  }
  return true;
}
int main() {
  int mid, ans;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int l = 0, r = m;
  while (l <= r) {
    mid = l + (r - l) / 2;
    if (check(mid)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
