#include <bits/stdc++.h>
using namespace std;
int a[300005];
int n, m;
bool solve(int k) {
  int mi = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] + k < m) {
      mi = max(mi, a[i]);
      if (mi > a[i] + k) return 0;
    } else {
      if (mi <= (a[i] + k) % m) {
      } else {
        mi = max(mi, a[i]);
      }
    }
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int L = 0, R = m - 1;
  while (L < R) {
    int mid = (L + R) / 2;
    if (solve(mid)) {
      R = mid;
    } else {
      L = mid + 1;
    }
  }
  cout << L << endl;
  return 0;
}
