#include <bits/stdc++.h>
using namespace std;
const int N = 300001;
int n;
int m;
int low;
int high;
int arr[N];
bool ok(int curr) {
  int tmp = m - 1;
  for (int i = n - 1; i >= 0; i--) {
    if (arr[i] <= tmp) {
      tmp = min(tmp, arr[i] + curr);
    } else if ((arr[i] + curr) >= m) {
      tmp = min(tmp, arr[i] + curr - m);
    } else {
      return false;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  high = m;
  while (low < high) {
    int mid = (low + high) >> 1;
    if (!ok(mid)) {
      low = mid + 1;
    } else {
      high = mid;
    }
  }
  cout << low << endl;
  return 0;
}
