#include <bits/stdc++.h>
using namespace std;
bool judge(vector<int> v, int n, int m, int k) {
  bool ok_flag = true;
  int last = 0;
  for (int i = 0; i < n; ++i) {
    int num = v[i];
    if ((num + k) % m >= last) {
      if (num + k >= m) {
        num = last;
      } else {
        num = (last > num ? last : num);
      }
    } else {
      if (num + k >= m) {
        num = (last > num ? last : num);
      } else {
        ok_flag = false;
        break;
      }
    }
    last = num;
  }
  return ok_flag;
}
int bs(vector<int> &v, int n, int m) {
  int l, r;
  l = 0;
  r = m;
  while (r > l) {
    int mid = (r + l) / 2;
    if (judge(v, n, m, mid)) {
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  if (!judge(v, n, m, l)) {
    ++l;
  }
  return l;
}
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> v;
  for (int i = 0; i < n; ++i) {
    int num;
    cin >> num;
    v.push_back(num);
  }
  int ans = bs(v, n, m);
  cout << ans << endl;
  return 0;
}
