#include <bits/stdc++.h>
using namespace std;
void preprocess() {}
long long n, m;
long long arr[500100];
long long fin(long long l, long long r, long long x) {
  if (x >= l && x <= r) {
    return x;
  } else if (x > r) {
    return INT_MAX;
  } else {
    return l;
  }
}
bool p(long long x) {
  long long mn = 0;
  for (long long i = 0; i < n; ++i) {
    long long left = (arr[i] + x) % m;
    long long right = arr[i];
    if (right > left) {
      long long p = fin(right, m - 1, mn);
      p = min(p, fin(0, left, mn));
      if (p == INT_MAX) {
        return false;
      }
      mn = p;
    } else {
      long long p = fin(right, left, mn);
      if (p == INT_MAX) {
        return false;
      }
      mn = p;
    }
  }
  return true;
}
long long binary_search(long long lo, long long hi) {
  while (lo < hi) {
    long long mid = lo + (hi - lo) / 2;
    if (p(mid) == true)
      hi = mid;
    else
      lo = mid + 1;
  }
  return lo;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  clock_t clk = clock();
  preprocess();
  long long t = 1;
  while (t--) {
    cin >> n >> m;
    for (long long i = 0; i < n; ++i) {
      cin >> arr[i];
    }
    cout << binary_search(0, m - 1) << "\n";
  }
  return 0;
}
