#include <bits/stdc++.h>
int n, m;
std::vector<int> a;
bool able(int);
int main() {
  scanf("%d%d", &n, &m);
  a.resize(n);
  for (auto& e : a) scanf("%d", &e);
  int left{-1}, right{m};
  while (right - left > 1) {
    int mid{(right + left) / 2};
    if (able(mid))
      right = mid;
    else
      left = mid;
  }
  printf("%d\n", right);
  return 0;
}
bool able(int count) {
  int prev{m - 1};
  for (int i{n - 1}; i >= 0; i--) {
    if (a[i] > prev) {
      if (a[i] + count < m) return false;
      prev = std::min(prev, (a[i] + count) % m);
    } else
      prev = std::min(prev, a[i] + count);
  }
  return true;
}
