#include <bits/stdc++.h>
using namespace std;
int a[300005], n, m;
bool solve(int x) {
  int y = 0;
  for (int i = 0; i < n; i++) {
    int need = y - a[i];
    if (need < 0) {
      need += m;
    }
    if (need > x) {
      if (y > a[i]) {
        return false;
      }
      y = a[i];
    }
  }
  return true;
}
int bin() {
  int l = 0, r = m;
  while (l < r) {
    int mid = (l + r) / 2;
    if (solve(mid)) {
      r = mid;
    } else {
      l = mid + 1;
    }
  }
  return l;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    cout << bin() << endl;
  }
  return 0;
}
