#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2, typename T3>
class triple {
 public:
  T1 first;
  T2 second;
  T3 third;
  triple() {
    first = 0;
    second = 0;
    third = 0;
  }
};
const int maxn = 3e5 + 5;
int n, m, l, r, mid;
int a[maxn];
bool check() {
  int temp1 = 0, temp2, temp3;
  bool flag = false;
  for (int((i)) = (0); ((i)) < ((n)); ((i))++) {
    temp2 = a[i];
    temp3 = a[i] + mid;
    if ((temp2 <= temp1 and temp1 <= temp3) or
        (temp2 <= temp1 + m and temp1 + m <= temp3))
      continue;
    if (temp2 < temp1) {
      flag = true;
      break;
    } else
      temp1 = temp2;
  }
  return flag;
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int((i)) = (0); ((i)) < ((n)); ((i))++) cin >> a[i];
  l = -1;
  r = m;
  while (l < r - 1) {
    mid = (l + r) / 2;
    if (check())
      l = mid;
    else
      r = mid;
  }
  cout << r << '\n';
  return 0;
}
