#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, m;
  cin >> n >> m;
  long long a[n];
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  long long f = 0;
  for (int i = 0; i < n - 1; ++i) {
    if (a[i] <= a[i + 1]) {
      f = 0;
    } else {
      f = 1;
      break;
    }
  }
  if (f == 0) {
    cout << 0 << endl;
    return 0;
  }
  long long l = 0, r = m, mid = (l + r) / 2;
  while (l < r) {
    mid = (l + r) / 2;
    long long flag = 0;
    std::vector<long long> v;
    for (int i = 0; i < n; ++i) {
      v.push_back(a[i]);
    }
    if (v[n - 1] + mid >= m) {
      v[n - 1] = m - 1;
    } else {
      v[n - 1] = v[n - 1] + mid;
    }
    for (int i = n - 2; i >= 0; i--) {
      if (v[i] <= v[i + 1]) {
        if (v[i] + mid >= v[i + 1]) {
          v[i] = v[i + 1];
          continue;
        }
        if (v[i] + mid < v[i + 1]) {
          v[i] = v[i] + mid;
        }
      } else {
        if (v[i] + mid < m) {
          flag = 1;
          break;
        } else {
          v[i] = min(v[i + 1], (v[i] + mid) % m);
        }
      }
    }
    if (flag == 1) {
      l = mid + 1;
      continue;
    } else {
      r = mid;
      continue;
    }
  }
  cout << l << endl;
  return 0;
}
