#include <bits/stdc++.h>
using namespace std;
const int N = 300003;
int n, m, arr[N];
bool can(int mid) {
  int last = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] > last) {
      int diff = last + m - arr[i];
      if (diff > mid) last = arr[i];
    } else {
      int diff = last - arr[i];
      if (diff > mid) return false;
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
  int left = 0, right = m, ans = -1;
  while (left <= right) {
    int mid = (left + right) >> 1;
    if (can(mid)) {
      ans = mid, right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  printf("%d", ans);
  return 0;
}
