#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[300005];
int temp[300005];
bool ok(int k) {
  for (int i = 0; i < n; i++) arr[i] = temp[i];
  if (k >= m - arr[0]) arr[0] = 0;
  int last = arr[0];
  for (int i = 1; i < n; i++) {
    int cur = arr[i];
    if (cur == last) {
    } else if (cur > last) {
      int d = m - 1 - cur + last + 1;
      if (k >= d) arr[i] = last;
    } else {
      int d = last - cur;
      if (k >= d)
        arr[i] = last;
      else
        return false;
    }
    last = arr[i];
  }
  return true;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", &temp[i]);
  int low = 0, high = 3 * m, best = -1;
  while (low <= high) {
    int mid = (low + high) / 2;
    if (ok(mid)) {
      best = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << best;
}
