#include <bits/stdc++.h>
//#include "grader.cpp"
using namespace std;
const int MAXN = ( 1e7 ) + 7 ;
int n;
long long l,d[ MAXN] ,p[ MAXN] ;
long long dp[ MAXN] ,ans;
long long percorri( int N, long long L, long long D[ ] , long long P[ ] ) {
int i,j;
n= N;
l= L;
for ( i= 1 ; i<= n; i++ ) d[ i] = D[ i- 1 ] ,p[ i] = P[ i- 1 ] ;
for ( i= n; i>= 1 ; i-- ) {
dp[ i] = l- d[ i] + p[ i] ;
for ( j= i+ 1 ; j<= n; j++ ) {
dp[ i] = min( dp[ i] ,max( d[ j] - d[ i] + p[ i] + p[ j] ,dp[ j] ) ) ;
}
}
ans= l;
for ( i= 1 ; i<= n; i++ ) ans= min( ans,max( d[ i] + p[ i] ,dp[ i] ) ) ;
return ans;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Ci8vI2luY2x1ZGUgImdyYWRlci5jcHAiCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1BWE4gPSAoMWU3KSArIDc7CgppbnQgbjsKbG9uZyBsb25nIGwsZFtNQVhOXSxwW01BWE5dOwpsb25nIGxvbmcgZHBbTUFYTl0sYW5zOwoKbG9uZyBsb25nIHBlcmNvcnJpKGludCBOLCBsb25nIGxvbmcgTCwgbG9uZyBsb25nIERbXSwgbG9uZyBsb25nIFBbXSkgewogICAgaW50IGksajsKICAgIG49TjsKICAgIGw9TDsKICAgIGZvcihpPTE7aTw9bjtpKyspIGRbaV09RFtpLTFdLHBbaV09UFtpLTFdOwogICAgZm9yKGk9bjtpPj0xO2ktLSkgewogICAgICAgIGRwW2ldPWwtZFtpXStwW2ldOwogICAgICAgIGZvcihqPWkrMTtqPD1uO2orKykgewogICAgICAgICAgICBkcFtpXT1taW4oZHBbaV0sbWF4KGRbal0tZFtpXStwW2ldK3Bbal0sZHBbal0pKTsKICAgICAgICB9CiAgICB9CiAgICBhbnM9bDsKICAgIGZvcihpPTE7aTw9bjtpKyspIGFucz1taW4oYW5zLG1heChkW2ldK3BbaV0sZHBbaV0pKTsKICAgIHJldHVybiBhbnM7Cn0=