#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int a[N];
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  int s = 0, e = m;
  while (s < e) {
    int mid = (s + e) >> 1;
    int mx = 0;
    bool b = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] + mid < m) {
        if (a[i] + mid < mx) {
          b = 1;
          break;
        }
        mx = max(mx, a[i]);
      } else if (mx > (a[i] + mid) % m)
        mx = max(mx, a[i]);
    }
    if (b)
      s = mid + 1;
    else
      e = mid;
  }
  cout << e;
  return 0;
}
