#include <bits/stdc++.h>
using namespace std;
int mod, n;
bool possible(int x, vector<int> arr) {
  if (arr[0] + x >= mod) {
    arr[0] = 0;
  }
  for (int i = 1; i < n; i++) {
    if (arr[i] < arr[i - 1]) {
      if (arr[i] + x < arr[i - 1]) {
        return false;
      }
      arr[i] = arr[i - 1];
    } else {
      if ((arr[i] + x) % mod >= arr[i - 1] && (arr[i] + x) % mod < arr[i]) {
        arr[i] = arr[i - 1];
      }
    }
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  cin >> n >> mod;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  int left = 0, right = mod - 1;
  while (left < right) {
    int x = (left + right) / 2;
    if (possible(x, arr)) {
      right = x;
    } else {
      left = x + 1;
    }
  }
  cout << left;
  return 0;
}
