#include <bits/stdc++.h>
using namespace std;
int n, m;
int a[300010];
int temp[300010];
int dp[300010][2];
int getCost(int a, int b) {
  if (a <= b) {
    return b - a;
  }
  return m - a + b;
}
bool solution(int value) {
  temp[0] = 0;
  for (int i = 1; i <= n; i++) {
    int costFront = getCost(a[i], temp[i - 1]);
    if (costFront <= value) {
      temp[i] = temp[i - 1];
    } else {
      temp[i] = a[i];
    }
  }
  for (int i = 1; i <= n; i++) {
    if (temp[i] < temp[i - 1]) {
      return false;
    }
  }
  return true;
}
int main() {
  memset(dp, -1, sizeof(dp));
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  int L = 0;
  int R = m;
  while (L < R) {
    int mid = (L + R) >> 1;
    if (solution(mid)) {
      R = mid;
    } else {
      L = mid + 1;
    }
  }
  printf("%d\n", L);
  return 0;
}
