#include <bits/stdc++.h>
using namespace std;
int a[300006];
int n, m;
bool check(int x) {
  int top;
  if (a[1] + x >= m) {
    top = 0;
  } else
    top = a[1];
  for (int i = 2; i <= n; i++) {
    if (a[i] >= top) {
      if (a[i] + x >= m && (a[i] + x) % m >= top)
        continue;
      else
        top = a[i];
    } else if (a[i] < top && a[i] + x >= top) {
      continue;
    } else
      return 0;
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int l = 0;
  int r = 1e8;
  int ans = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid - 1;
      ans = mid;
    } else
      l = mid + 1;
  }
  printf("%d\n", ans);
}
