#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, i, mx = 0, m, ans = 1e9;
  cin >> n >> m;
  long long int a[n], c[n];
  for (i = 0; i < n; i++) {
    cin >> c[i];
  }
  long long int l = 0, r = m, mid;
  while (l <= r) {
    for (i = 0; i < n; i++) {
      a[i] = c[i];
    }
    int first = 0;
    mid = (l + r) / 2;
    if (a[0] > (a[0] + mid) % m) a[0] = 0;
    for (i = 1; i < n; i++) {
      if (a[i] < a[i - 1]) {
        if (a[i - 1] - a[i] <= mid) {
          a[i] = a[i - 1];
        } else {
          first = 1;
          break;
        }
      }
      if (a[i] > a[i - 1]) {
        if (a[i] > (a[i] + mid) % m && (a[i] + mid) % m >= a[i - 1]) {
          a[i] = a[i - 1];
        }
      }
    }
    if (first == 0) {
      ans = min(ans, mid);
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << ans;
}
