#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  int lo = 0, hi = m;
  while (lo < hi) {
    int mid = (lo + hi) / 2;
    int last = 0;
    bool b = true;
    for (int i = 0; i < n; ++i) {
      int delta = (last - a[i] + m) % m;
      if (delta <= mid) continue;
      if (a[i] < last) {
        b = false;
        break;
      }
      last = a[i];
    }
    if (b) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << '\n';
  return 0;
}
