#include using namespace std; #define ll long long #define FOR(i,n,m) for(int i=(n);i<(m);i++) #define REP(i,n) for(int i=0;i<(n);i++) #define REPR(i,n) for(int i=(n);i>=0;i--) #define all(vec) vec.begin(),vec.end() using vi=vector; using vvi=vector; using vl=vector; using vvl=vector; using P=pair; using PP=pair; using vp=vector

; using vpp=vector; using vs=vector; #define fi first #define se second #define pb push_back templatebool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;} templateistream&operator>>(istream&is,pair &p){is>>p.fi>>p.se;return is;} templateostream&operator<<(ostream&os,const pair &p){os<<"("<istream&operator>>(istream&is,vector &t){REP(i,t.size())is>>t[i];return is;} templateostream&operator<<(ostream&os,const vector&t){os<<"{";REP(i,t.size()){if(i)os<<",";os<>n>>l; vi x(n); cin>>x; vi y(n); cin>>y; set a,b; REP(i,n){ a.insert(x[i]); a.insert(x[i]+l); a.insert(x[i]+l*2); b.insert(y[i]); b.insert(y[i]+l); b.insert(y[i]+l*2); } int now=0; ll ans=0; REP(i,n){ if(i==n-1){ int p=*a.lower_bound(now); ans+=p-now; now=p%l; int q=*b.lower_bound(now); ans+=q-now; }else{ int p=*a.lower_bound(now); int q=*b.upper_bound(p); int r=*(--a.upper_bound(q)); a.erase(r%l); a.erase(r%l+l); a.erase(r%l+l*2); ans+=r-now; now=r%l; p=*b.lower_bound(now); q=*a.upper_bound(p); r=*(--b.upper_bound(q)); b.erase(r%l); b.erase(r%l+l); b.erase(r%l+l*2); ans+=r-now; now=r%l; } } cout<