#include <bits/stdc++.h>
using namespace std;
int n, m;
const int N = 300002;
int a[N];
bool can(int x) {
  int last = a[0];
  if (a[0] + x >= m) last = 0;
  for (int i = 1; i < n; i++) {
    int curr = a[i];
    if (curr <= last) {
      if (last - curr > x) {
        return false;
      } else {
      }
    } else {
      if ((curr + x) % m >= last) {
        if (curr + x >= m) {
        } else {
          last = curr;
        }
      } else {
        last = curr;
      }
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cout.precision(12);
  cout << fixed;
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l = 0;
  int r = m;
  while (l + 1 != r) {
    int mid = (l + r) / 2;
    if (can(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  if (can(l)) r = l;
  cout << r << endl;
  return 0;
}
