#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 300;
int a[N], b[N];
int n, m;
int check(int x) {
  int y = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] <= y) {
      if (y - a[i] <= x) {
        b[i] = y;
      } else {
        return false;
      }
    } else {
      if (m - a[i] + y <= x) {
        b[i] = y;
      } else {
        b[i] = a[i];
      }
    }
    y = b[i];
  }
  return true;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l = -1, r = m;
  while (r - l > 1) {
    int mid = (l + r) / 2;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << " ";
}
