#include <bits/stdc++.h>
using namespace std;
long long n, m;
long long check(long long x, vector<long long> v) {
  long long mx = 0;
  for (long long &a : v) {
    if (a < mx) {
      if (mx - a > x) return 0;
    } else {
      if (m - a + mx > x) mx = a;
    }
  }
  return 1;
}
int main() {
  cin >> n >> m;
  vector<long long> a(n);
  for (long long &x : a) cin >> x;
  long long lw = 0, hg = m - 1;
  while (hg - lw > 10) {
    long long mid = (lw + hg) >> 1;
    if (check(mid, a))
      hg = --mid;
    else
      lw = ++mid;
  }
  for (int i = lw; i <= hg; i++)
    if (check(i, a)) {
      cout << i;
      return 0;
    }
}
