#include <bits/stdc++.h>
using namespace std;
const int M = 300000 + 5;
int n, m;
int a[M];
int pre[M];
int slove(int x, int y) {
  if (x <= y)
    return y - x;
  else
    return m - x + y;
}
int check(int x) {
  for (int i = 1; i <= n; i++) {
    a[i] = pre[i];
  }
  for (int i = 1; i <= n; i++) {
    int k = slove(a[i], a[i - 1]);
    if (k > x) {
      if (a[i] < a[i - 1]) return 0;
    } else {
      a[i] = a[i - 1];
    }
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(false);
  ;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> pre[i];
  a[0] = 0;
  int l = 0, r = 300000;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
