#include <bits/stdc++.h>
using namespace std;
int a[300300], n, m;
bool check(int x) {
  int pre = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] > pre) {
      if (a[i] + x >= m && (a[i] + x) % m >= pre) {
        continue;
      } else {
        pre = a[i];
      }
    } else if (a[i] < pre) {
      if (a[i] + x < pre) {
        return false;
      }
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  int l = 0, r = m, x;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      x = mid;
      r = mid - 1;
    } else {
      l = mid + 1;
    }
  }
  cout << x << endl;
}
