#include <bits/stdc++.h>
using namespace std;
long long n, m;
vector<long long> arr;
bool poss(long long a) {
  long long mn, mx, p = arr[0];
  if (arr[0] > (arr[0] + a) % m) p = 0;
  for (long long i = 1; i < n; i++) {
    if (p > min(arr[i] + a, m - 1)) return false;
    if (arr[i] + a >= m && (arr[i] + a) % m >= p)
      continue;
    else
      p = max(arr[i], p);
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  cin >> n >> m;
  arr.resize(n);
  for (long long i = 0; i < n; i++) cin >> arr[i];
  long long l = 0, h = m - 1, mid = (l + h) / 2;
  while (l < h) {
    if (poss(mid))
      h = mid;
    else
      l = mid + 1;
    mid = (l + h) / 2;
  }
  cout << l << "\n";
  return 0;
}
