#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const long long MOD = 1e9 + 7;
int n, m;
vector<int> a;
bool check(int x) {
  int mn = -1;
  for (int i = 0; i < n; i++) {
    if (a[i] + x >= m) {
      int pos = (a[i] + x) % m;
      if (pos < mn) {
        mn = max(mn, a[i]);
      }
      mn = max(mn, 0);
    } else {
      if (a[i] + x < mn) {
        return 0;
      }
      mn = max(mn, a[i]);
    }
  }
  return 1;
}
signed main() {
  cin >> n >> m;
  a.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int l = -1, r = m;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (check(mid)) {
      r = mid;
    } else {
      l = mid;
    }
  }
  cout << r << "\n";
  return 0;
}
