#include <bits/stdc++.h>
using namespace std;
void inout() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
}
bool valid(vector<long long> &a, long long x, long long m) {
  long long n = a.size(), cur = 0, i;
  vector<long long> r(n);
  for (i = 0; i < n; i++) r[i] = (a[i] + x) % m;
  for (i = 0; i < n; i++) {
    if (r[i] >= a[i]) {
      if (cur > r[i]) return 0;
      cur = min(max(cur, a[i]), r[i]);
    } else if (cur > r[i])
      cur = max(cur, a[i]);
  }
  return 1;
}
int main() {
  inout();
  long long n, m, l, r, mid, ans = m;
  cin >> n >> m;
  vector<long long> a(n);
  for (auto &i : a) cin >> i;
  l = 0, r = m;
  while (l <= r) {
    mid = (l + r) / 2;
    if (valid(a, mid, m))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  cout << ans;
  return 0;
}
