#include <bits/stdc++.h>
using namespace std;
long long n, m;
vector<long long> a(30005);
bool check(long long v) {
  long long i, val = 0;
  for (i = 0; i < n; i++) {
    long long x = a[i];
    long long y = (a[i] + v) % m;
    if (y >= x) {
      if (x > val) val = x;
      if (y < val) return false;
    } else {
      if (val > y) val = max(val, x);
    }
  }
  return true;
}
void solve() {
  cin >> n >> m;
  a.resize(n);
  for (auto &x : a) cin >> x;
  long long low = 0, high = m, mid, ans = m;
  while (low <= high) {
    mid = (low + high) / 2;
    long long z = check(mid);
    if (z) {
      ans = mid;
      high = mid - 1;
    } else
      low = mid + 1;
  }
  cout << ans << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
