#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 5;
const int mod = 1000000007;
vector<int> v1, v2;
int a[N];
map<int, int> mp;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int i, j, n, m, k, d, x, y, z;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  int l = 0, r = m - 1, mid, prev, flag = 0;
  while (l < r) {
    mid = (l + r) / 2;
    flag = 0;
    if (a[0] + mid >= m)
      prev = 0;
    else
      prev = a[0];
    for (int i = 1; i < n; i++) {
      if (a[i] > prev) {
        if (a[i] + mid < m + prev) prev = a[i];
      } else if (prev - a[i] > mid) {
        flag = 1;
        break;
      }
    }
    if (flag)
      l = mid + 1;
    else
      r = mid;
  }
  cout << l << endl;
  return 0;
}
