#include using namespace std; long N,L,a,s,t,c,i,x[100001],y[100001],*p=&x[0],*q=&y[0]; int main(){cin>>N>>L;for(i=0;i!=N;++i)cin>>x[i];for(i=0;i!=N;++i)cin>>y[i];x[N]=L;y[N]=L;for(i=0;i!=N*2;++i){if(*p<*q){++c;if(s<=c)s=c,i=*q;++p;}else{--c;if(c<=t)t=c;++q;}}cout<<(s?L*(s-t)-L+i:L*(s-t)+y[0]);}