#include <bits/stdc++.h>
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const long long int int_max = 1e18;
const long long int int_min = -1e18;
const long double PI = acos(-1);
using namespace std;
long long int n, m;
long long int a[N];
bool check(long long int ans) {
  long long int prev = 0;
  for (long long int i = 1; i <= n; i++) {
    if (a[i] == prev) {
      continue;
    }
    if (a[i] > prev) {
      long long int steps = m - a[i] + prev;
      if (steps <= ans) {
        continue;
      } else {
        prev = a[i];
      }
    } else {
      long long int steps = prev - a[i];
      if (steps <= ans) {
        continue;
      } else {
        return false;
      }
    }
  }
  return true;
}
void solve() {
  cin >> n >> m;
  for (long long int i = 1; i <= n; i++) cin >> a[i];
  int flg = 0;
  for (long long int i = 1; i <= n - 1; i++) {
    if (a[i] > a[i + 1]) {
      flg = 1;
      break;
    }
  }
  if (flg == 0) {
    cout << 0 << '\n';
    return;
  }
  long long int l = 1, r = INT_MAX;
  while (l <= r) {
    long long int mid = (l + r) / 2;
    if (check(mid)) {
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << l << '\n';
}
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL);
  solve();
}
