#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
long long int n, m, a[N], l[N], r[N];
bool ok(long long int x) {
  for (int i = 0; i < n; i++) {
    l[i] = a[i];
    r[i] = (a[i] + x) % m;
  }
  long long int prev = 0;
  for (int i = 0; i < n; i++) {
    if (r[i] >= l[i]) {
      if (prev > r[i]) {
        return false;
      }
      prev = max(prev, l[i]);
    } else {
      if (prev > r[i]) {
        prev = max(prev, l[i]);
      }
    }
  }
  return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int ans = 0, low = 0, high = m - 1;
  while (low < high) {
    long long int mid = (low + high) / 2;
    if (ok(mid)) {
      high = mid;
    } else {
      low = mid + 1;
    }
  }
  ans = low;
  cout << ans << "\n";
  return 0;
}
