#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, m;
int a[300003];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l = 0;
  int r = m;
  while (l < r) {
    int mid = (l + r) / 2;
    int pos = (a[0] + mid >= m ? 0 : a[0]);
    int cur = 1;
    for (int i = 1; i < n; i++) {
      if (a[i] + mid < pos) {
        cur = 0;
        break;
      }
      if (a[i] + mid < m + pos) {
        pos = max(pos, a[i]);
      }
    }
    if (cur)
      r = mid;
    else
      l = mid + 1;
  }
  cout << l;
}
