#include <bits/stdc++.h>
using namespace std;
const int MAXN = 3e5 + 10;
const int mod = 332748118;
int n, m;
int a[MAXN];
bool check(int mid) {
  int pre = 0;
  for (int i = 1; i <= n; ++i) {
    if (a[i] + mid < pre) return false;
    if (a[i] > pre) {
      if ((a[i] + mid) % m >= pre && a[i] + mid >= m)
        continue;
      else
        pre = a[i];
    }
  }
  return true;
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  int l = 0, r = m;
  int res = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid - 1;
      res = mid;
    } else
      l = mid + 1;
  }
  cout << res << endl;
  return 0;
}
