#include <bits/stdc++.h>
using namespace std;
bool ok(long long int x, vector<long long int> &a, long long int n,
        long long int m) {
  long long int mbg = 0;
  for (long long int i = 0; i < n; i++) {
    if (a[i] > mbg) {
      long long int op = m - a[i] + mbg;
      if (op > x) {
        mbg = a[i];
      } else {
      }
    } else if (a[i] < mbg) {
      long long int op = mbg - a[i];
      if (op > x) {
        return false;
      }
    }
  }
  return true;
}
void solve() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> a(n);
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int low = 0, high = m - 1, ans = 0;
  while (low <= high) {
    long long int mid = (high - low) / 2 + low;
    if (ok(mid, a, n, m)) {
      high = mid - 1;
      ans = mid;
    } else {
      low = mid + 1;
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
