#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[300020];
bool check(int x) {
  int last = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] > last) {
      if (arr[i] + x < last + m) last = arr[i];
    } else if (arr[i] < last) {
      if (arr[i] + x < last) return 0;
    }
  }
  return 1;
}
int bs() {
  int l = 0, r = m + 1;
  while (l < r) {
    int mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid + 1;
  }
  return l;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> arr[i];
  cout << bs() << '\n';
  return 0;
}
