#include <bits/stdc++.h>
using namespace std;
long long ab(long long x) { return x > 0LL ? x : -x; }
long long n, m;
vector<long long> v;
bool ok(long long k) {
  long long p = 0;
  for (long long i = 0; i < n; i++) {
    if (v[i] + k < m) {
      if (v[i] + k < p) return 0;
      p = max(p, v[i]);
    } else if (v[i] + k - m < p)
      p = max(p, v[i]);
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 0; i < n; i++) {
    long long x;
    cin >> x;
    v.push_back(x);
  }
  long long l = -1, r = m;
  while (r - l > 1) {
    long long mid = (l + r) / 2;
    if (ok(mid))
      r = mid;
    else
      l = mid;
  }
  cout << r << '\n';
  return 0;
}
