#include <iostream>
#include <queue>
using namespace std;

int main(){
	int n,lim,*ice,buf,pg,ov,sz,h=0;
	queue<int> q;
	
	cin >> n >> lim;
	
	ice = new int[n+2];
	ice[0] = ice[n+1] = 0;
	for(int i = 1; i < n+1; i++){
		cin >> ice[i];
	}
	
	for(int i = 1; i < n+1; i++){
		if((ice[i] > ice[i-1]) && (ice[i] > ice[i+1])){
			q.push(i);
		}
	}
	
	while(1){
		buf = q.front();
		pg = (lim - ice[buf]) > 0?(lim - ice[buf]):0;
		
		sz = q.size();
		for(int i = 0; i < sz; i++){
			buf = q.front();
			q.pop();
			
			ice[buf] += pg;
			ov = (ice[buf] - lim) >= 0?(ice[buf] - lim):-1;
			
			if(ov != -1){
				ice[buf] = 0;
				
				if(ice[buf-1] > ice[buf-2]){
					ice[buf-1] += ov;
					q.push(buf-1);
				}
				if(ice[buf+1] > ice[buf+2]){
					ice[buf+1] += ov;
					q.push(buf+1);
				}
			}else{
				q.push(buf);
			}
		}
		h += pg;
		
		if(q.empty()){
			break;
		}
	}
	
	cout << h << endl;
	
	return 0;
}