#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
constexpr int N = 3e5 + 5;
int n, m;
int arr[N];
bool check(int k) {
  int cur = 0, temp;
  bool f = true;
  if (arr[0] + k < m) cur = arr[0];
  for (int i = 1; i < n; i++) {
    if (arr[i] + k < cur) {
      f = false;
      break;
    }
    if (arr[i] > cur) {
      temp = cur + m - arr[i];
      if (temp > k) cur = arr[i];
    }
  }
  return f;
}
void Solve() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> arr[i];
  int l = 0, r = m, mid, ans = m - 1;
  while (l < r) {
    mid = (l + r) / 2;
    if (check(mid)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  if (check(ans - 1)) ans--;
  ans = max(ans, 0);
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  Solve();
  return 0;
}
