#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> b;
bool isSolvable(int moves) {
  vector<int> a(b);
  if (a[0] + moves >= m) a[0] = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      if (a[i - 1] - a[i] <= moves)
        a[i] = a[i - 1];
      else
        return false;
    } else {
      int temp = (a[i] + moves) % m;
      if (a[i - 1] <= temp && temp < a[i]) a[i] = a[i - 1];
    }
  }
  return true;
}
int binarySearch(int start, int end) {
  if (end == start) return end;
  int mid = (end + start) / 2;
  return isSolvable(mid) ? binarySearch(start, mid)
                         : binarySearch(mid + 1, end);
}
int main() {
  cin >> n >> m;
  b.resize(n);
  for (int i = 0; i < n; i++) cin >> b[i];
  cout << binarySearch(0, m) << endl;
  return 0;
}
