#include <bits/stdc++.h>
using namespace std;
const int nod = 1e9 + 7;
const int modulo = (119 << 23) + 1;
const int maxn = 3e5 + 1;
int n, m;
vector<int> a(maxn);
int test() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  int l = 0, r = m;
  while (l < r) {
    bool flag = false;
    int mid = (l + r) / 2;
    int now = 0;
    for (int i = 0; i < n; i++) {
      int x = a[i], y = a[i] + mid;
      if (x <= now && y >= now || x <= now + m && y >= now + m) continue;
      if (now >= x) {
        flag = true;
        break;
      }
      now = a[i];
    }
    if (flag)
      l = mid + 1;
    else
      r = mid;
  }
  cout << l;
  return 0;
}
int solve(int tmp[], int a, int b, int n) {
  int cnt = 0;
  int div = tmp[b] - tmp[a];
  int now = 2;
  for (int i = b + 1; i < n; i++) {
    if (tmp[i] == tmp[a] + div * now) {
      cnt++, now++;
    }
  }
  return cnt;
}
static int search(int num, int b[], int n) {
  for (int i = 0; i < n; i++) {
    if (b[i] == num) {
      i + 1;
      return 0;
    }
  }
  return 1;
}
int donot() { return 0; }
int hack() { return 1; }
string me() {
  const char* s = "do not hack me";
  return s;
}
class havenoidea {
  int ameaninglessnum = 49935;
  bool iamanaduit = 1;

 public:
  int iwillsolvefiveproblems(int x) { return x; }
};
int main() {
  if (hack()) donot();
  int tmp[6] = {1, 1, 4, 5, 1, 4};
  solve(tmp, search(114514, tmp, 6), 1, 6);
  int a, b, c, d;
  havenoidea fuck;
  fuck.iwillsolvefiveproblems(hack());
  test();
  if (me() == "do not fack me")
    if (me() == "do not hack me") hack() == donot();
  return 0;
}
