#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
bool check(vector<int> &vec, int n, int m, int ans) {
  int cur_h = vec[0];
  if (vec[0] + ans >= m) cur_h = 0;
  for (int i = 0; i < n; i++) {
    if (vec[i] < cur_h) {
      if (cur_h - vec[i] > ans) return false;
    } else if (vec[i] > cur_h) {
      if (((vec[i] + ans) % m) >= cur_h && ((vec[i] + ans) % m) < vec[i])
        continue;
      cur_h = vec[i];
    }
  }
  return true;
}
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> vec(n);
  for (int i = 0; i < n; i++) cin >> vec[i];
  int l = 0;
  int r = m;
  int mid;
  while (l < r - 1) {
    mid = (l + r) / 2;
    if (check(vec, n, m, mid)) {
      r = mid;
    } else
      l = mid + 1;
  }
  if (check(vec, n, m, min(l, r)))
    cout << min(l, r);
  else
    cout << max(l, r);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  solve();
  return 0;
}
