#include <bits/stdc++.h>
using namespace std;
int n, m, d[300005], x;
bool check(int a) {
  x = 0;
  for (int i = 1; i <= n; i++) {
    if (d[i] + a >= m) {
      if ((d[i] + a) % m >= x) continue;
      x = max(d[i], x);
      continue;
    }
    if (d[i] + a >= x) {
      x = max(d[i], x);
      continue;
    }
    return true;
  }
  return false;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> d[i];
  }
  int l = 0, r = m - 1;
  while (l < r) {
    int mid = (l + r) / 2;
    if (check(mid) == false) {
      r = mid;
    } else
      l = mid + 1;
  }
  cout << l << endl;
}
