#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1;
vector<int> v;
int n, m;
bool check(int k) {
  int temp;
  if (v[0] + k >= m)
    temp = 0;
  else
    temp = v[0];
  for (int i = 1; i < v.size(); ++i) {
    if (temp > v[i] + k) return false;
    if (v[i] > temp && (v[i] + k < m || (v[i] + k) % m < temp)) temp = v[i];
  }
  return true;
}
int bins(int l, int r) {
  if (l == r) return l;
  int mid = (l + r) / 2;
  if (check(mid))
    return bins(l, mid);
  else
    return bins(mid + 1, r);
}
int main() {
  int t;
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> t;
    v.push_back(t);
  }
  cout << bins(0, m);
  return 0;
}
