#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<= i+ 10 ; 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+Ci8vI2luY2x1ZGUgImdyYWRlci5jcHAiCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IE1BWE4gPSAoMWU3KSArIDc7CgppbnQgbjsKbG9uZyBsb25nIGwsZFtNQVhOXSxwW01BWE5dOwpsb25nIGxvbmcgZHBbTUFYTl0sYW5zOwoKbG9uZyBsb25nIHBlcmNvcnJpKGludCBOLCBsb25nIGxvbmcgTCwgbG9uZyBsb25nIERbXSwgbG9uZyBsb25nIFBbXSkgewogICAgaW50IGksajsKICAgIG49TjsKICAgIGw9TDsKICAgIGZvcihpPTE7aTw9bjtpKyspIGRbaV09RFtpLTFdLHBbaV09UFtpLTFdOwogICAgZm9yKGk9bjtpPj0xO2ktLSkgewogICAgICAgIGRwW2ldPWwtZFtpXStwW2ldOwogICAgICAgIGZvcihqPWkrMTtqPD1uICYmIGo8PWkrMTA7aisrKSB7CiAgICAgICAgICAgIGRwW2ldPW1pbihkcFtpXSxtYXgoZFtqXS1kW2ldK3BbaV0rcFtqXSxkcFtqXSkpOwogICAgICAgIH0KICAgIH0KICAgIGFucz1sOwogICAgZm9yKGk9MTtpPD1uO2krKykgYW5zPW1pbihhbnMsbWF4KGRbaV0rcFtpXSxkcFtpXSkpOwogICAgcmV0dXJuIGFuczsKfQ==