#include <bits/stdc++.h>
using namespace std;
long long int n, m;
vector<long long int> qn(300009);
bool che(long long int mid) {
  vector<long long int> temp = qn;
  if (temp[0] + mid >= m) temp[0] = 0;
  for (long long int i = 1; i <= n - 1; i++) {
    if (temp[i] < temp[i - 1] && temp[i] + mid < temp[i - 1])
      return false;
    else if (temp[i] < temp[i - 1] && temp[i] + mid >= temp[i - 1]) {
      temp[i] = temp[i - 1];
    } else if (temp[i] == temp[i - 1])
      continue;
    else if (temp[i] > temp[i - 1] && (temp[i] + mid) % m >= temp[i - 1] &&
             temp[i] + mid >= m) {
      temp[i] = temp[i - 1];
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long int i = 0; i <= n - 1; i++) cin >> qn[i];
  long long int lo = 0;
  long long int hi = m;
  long long int mid;
  while (lo < hi) {
    mid = lo + (hi - lo) / 2;
    if (che(mid)) {
      hi = mid;
    } else {
      lo = mid + 1;
    }
  }
  cout << lo << endl;
  return 0;
}
