#include <bits/stdc++.h>
using namespace std;
template <typename T>
static ostream& operator<<(ostream& stream, const vector<T>& t);
template <typename T>
static istream& read(T, T, istream& = cin);
template <typename T>
static istream& operator>>(istream& stream, vector<T>& t) {
  return read((t).begin(), (t).end(), stream);
}
template <typename T>
static istream& read(T b, T e, istream& stream) {
  for (T it = b; it != e; ++it) stream >> *it;
  return stream;
}
template <typename T>
static void print(T x, string end = "\n") {
  cout << x << end;
}
const int N = 3e5 + 5;
long long sum[N], cnt[N];
long long fget(long long ftree[], int x) {
  long long ans = 0;
  for (int i = x; i > 0; i -= i & (-i)) ans += ftree[i];
  return ans;
}
void fadd(long long ftree[], int x, int v) {
  for (int i = x; i < N; i += i & (-i)) ftree[x] += v;
}
int n, m;
bool solve(vector<int> arr, int k) {
  int v = 0;
  for (int x : arr) {
    if (x + k < v) return false;
    if (x + k >= v + m) continue;
    if (x > v) {
      v = x;
      continue;
    }
  }
  return true;
}
void _() {
  cin >> n >> m;
  vector<int> arr(n);
  cin >> arr;
  int lo = 0, hi = m, mid;
  while (lo < hi) {
    mid = (lo + hi) / 2;
    if (solve(arr, mid))
      hi = mid;
    else
      lo = mid + 1;
  }
  print(lo);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);
  _();
}
