#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[300001];
bool check(int val) {
  if (val < 0) return false;
  if (val >= m - 1) return true;
  int prev = 0;
  for (int i = int(1); i <= int(n); i++) {
    if (arr[i] + val < prev) return false;
    int r = (arr[i] + val) % m;
    if (r < arr[i]) {
      if (r >= prev)
        continue;
      else
        prev = (prev > arr[i] ? prev : arr[i]);
    } else {
      prev = (prev > arr[i] ? prev : arr[i]);
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = int(1); i <= int(n); i++) cin >> arr[i];
  int l = 0, r = m - 1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (check(mid) && (!check(mid - 1))) {
      cout << mid;
      return 0;
    } else if (!check(mid))
      l = mid + 1;
    else
      r = mid - 1;
  }
}
