#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, p, i, j, k, result;
  cin >> n >> p;
  int a[n + 1], b[n + 1];
  for (i = 1; i <= n; i = i + 1) cin >> a[i];
  i = 0;
  j = p - 1;
  result = p - 1;
  do {
    int m = (i + j) / 2;
    bool check = true;
    b[0] = 0;
    for (int k = 1; k <= n; k = k + 1) b[k] = 1000000000;
    for (int k = 1; k <= n; k = k + 1) {
      if (((a[k] + m) % p >= b[k - 1]) && (a[k] + m >= p)) b[k] = b[k - 1];
      if ((b[k - 1] - a[k] >= 0) && (b[k - 1] - a[k] <= m))
        b[k] = min(b[k], b[k - 1]);
      if (a[k] >= b[k - 1]) b[k] = min(b[k], a[k]);
      if (b[k] == 1000000000) {
        check = false;
        break;
      };
    }
    if (check == true) {
      j = m - 1;
      result = min(result, m);
    } else
      i = m + 1;
  } while (i <= j);
  cout << result;
}
