#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
const int inf = 1e9 + 7;
int a[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int l = 0, r = inf;
  while (l < r) {
    int mid = l + r >> 1;
    int k = 0, flag = 0;
    for (int i = 1; i <= n; i++) {
      int b = (a[i] + mid >= m) ? ((a[i] + mid) % m >= k ? k : max(k, a[i]))
                                : (k > a[i] + mid ? (flag = 1) : max(k, a[i]));
      if (flag) break;
      k = b;
    }
    if (flag)
      l = mid + 1;
    else
      r = mid;
  }
  cout << l << endl;
  return 0;
}
