#include <bits/stdc++.h>
using namespace std;
bool check(int step, int m, vector<int> v) {
  if (v[0] + step >= m) v[0] = 0;
  for (int i = 1; i < v.size(); i++) {
    if (v[i] < v[i - 1]) {
      if (v[i] + step >= v[i - 1]) v[i] = v[i - 1];
    } else {
      if (v[i] + step >= m && (v[i] + step) % m >= v[i - 1]) v[i] = v[i - 1];
    }
  }
  for (int i = 1; i < v.size(); i++) {
    if (v[i] < v[i - 1]) return false;
  }
  return true;
}
int main() {
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  vector<int> v;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    v.push_back(a);
  }
  if (n <= 1) {
    cout << "0" << endl;
    return 0;
  }
  int lo = 0, hi = m - 1;
  int ans = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (check(mid, m, v)) {
      ans = mid;
      hi = mid - 1;
    } else
      lo = mid + 1;
  }
  cout << ans << endl;
  return 0;
}
