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