#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int n, m, cnt, a[300002];
int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  int l = 0, r = m;
  while (r != l) {
    int cur = 0, mid = (l + r) / 2;
    bool ctrl = true;
    for (int i = 1; i <= n && ctrl; i++) {
      int mn = m, mx = 0;
      if (a[i] + mid >= m)
        mn = 0;
      else
        mn = a[i];
      if (a[i] + mid >= m - 1)
        mx = m - 1;
      else
        mx = a[i] + mid;
      if (mx < cur) {
        ctrl = false;
      } else if (a[i] > cur && mn == 0 && ((a[i] + mid) % m) < cur)
        cur = a[i];
      else if (a[i] > cur && mn != 0)
        cur = a[i];
    }
    if (ctrl)
      r = mid;
    else
      l = mid + 1;
  }
  cout << r;
}
