#include <bits/stdc++.h>
using namespace std;
long long int n, m, ans = 0;
long long int arr[300001];
void input() {
  cin >> n >> m;
  for (long long int i = 0; i < n; i++) {
    cin >> arr[i];
  }
}
long long int calcPrev(long long int prev, long long int cur,
                       long long int val) {
  if (cur < prev) {
    prev = prev;
  } else {
    long long int reqOps = m - cur + prev;
    if (reqOps > val) {
      prev = cur;
    }
  }
  return prev;
}
bool predicateFunction(long long int val) {
  long long int ok = 1;
  long long int prev = 0;
  for (long long int i = 0; i < n; i++) {
    if (arr[i] == prev) {
      prev = arr[i];
    } else if (arr[i] > prev) {
      prev = calcPrev(prev, arr[i], val);
    } else if (arr[i] < prev) {
      if (prev - arr[i] > val) {
        ok = 0;
        break;
      } else {
        prev = calcPrev(prev, arr[i], val);
      }
    }
  }
  return ok;
}
void solve() {
  long long int start = 0, end = m;
  while (start <= end) {
    long long int mid = (start + end) / 2;
    if (predicateFunction(mid)) {
      ans = mid;
      end = mid - 1;
    } else {
      start = mid + 1;
    }
  }
}
void printAnswer() { cout << ans << endl; }
int main() {
  input();
  solve();
  printAnswer();
}
