#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=300010;
int n,m;
int w[N];
bool check(int mid){
vector<int> t(n+1);
for(int i=0;i<n;++i) t[i]=w[i];
t[n]=m;
for(int i=n-1;i>=0;--i){
if(t[i+1]>=t[i]){
if(t[i]+mid>=m) t[i]=t[i+1];
else t[i]=min(t[i]+mid,t[i+1]);
}
else{
if(t[i]+mid>=m) t[i]=min(t[i+1],(t[i]+mid)%m);
else return false;
}
// cout<<t[i]<<" ";
}
// cout<<endl;
// for(int i=0;i<n;++i){
// if(w[i]>w[i+1]) return false;
// }
return true;
}
int main(){
#ifdef DEBUG
freopen("test.txt", "r", stdin);
#endif
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
for(int i=0;i<n;++i) cin>>w[i];
int l=0,r=m;
while(l<r){
int mid=l+r>>1;
if(check(mid)) r=mid;
else l=mid+1;
}
cout<<l;
// cout<<check(0)<<endl;
}
400B - Inna and New Matrix of Candies | 870B - Maximum of Maximums of Minimums |
1600E - Array Game | 1149A - Prefix Sum Primes |
277A - Learning Languages | 769A - Year of University Entrance |
1738A - Glory Addicts | 1738C - Even Number Addicts |
1064B - Equations of Mathematical Magic | 384A - Coder |
1738B - Prefix Sum Addicts | 1352D - Alice Bob and Candies |
1316D - Nash Matrix | 1548B - Integers Have Friends |
348A - Mafia | 315B - Sereja and Array |
204A - Little Elephant and Interval | 385B - Bear and Strings |
114C - Grammar Lessons | 1427A - Avoiding Zero |
583A - Asphalting Roads | 1358B - Maria Breaks the Self-isolation |
828A - Restaurant Tables | 1735A - Working Week |
1735D - Meta-set | 1735B - Tea with Tangerines |
1735C - Phase Shift | 1321C - Remove Adjacent |
281B - Nearest Fraction | 1043A - Elections |