#include <bits/stdc++.h>
using namespace std;
bool check(long long operations, long long m, vector<long long> &v) {
  vector<long long> temp;
  if (m - v[0] <= operations)
    temp.push_back(0);
  else
    temp.push_back(v[0]);
  for (long long i = 1; i < v.size(); i++) {
    if (v[i] <= temp.back()) {
      if (temp.back() - v[i] <= operations)
        temp.push_back(temp.back());
      else
        return false;
    } else {
      if (m - v[i] + temp.back() <= operations)
        temp.push_back(temp.back());
      else
        temp.push_back(v[i]);
    }
  }
  return true;
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  vector<long long> v(n);
  for (long long &i : v) cin >> i;
  long long l = 0, r = 1e18;
  while (l < r) {
    long long mid = (l + r) / 2;
    if (check(mid, m, v))
      r = mid;
    else
      l = mid + 1;
  }
  cout << r << "\n";
  return 0;
}
