#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll n, m;
vector<ll> a;
bool solve(ll x) {
  ll start = 0;
  for (auto i : a) {
    if (i <= start && i + x >= start) {
    } else if (start + m <= i + x) {
    } else if (i < start) {
      return false;
    } else {
      start = i;
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (ll i = 0; i < n; i++) {
    ll x;
    cin >> x;
    a.push_back(x);
  }
  ll ans = 1e9;
  ll low = 0, high = m - 1;
  while (low <= high) {
    ll mid = (low + high) >> 1;
    if (solve(mid)) {
      ans = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  cout << ans;
  return 0;
}
