#include #define int ll using namespace std; #define rep(i,n) for(int i=0;i=0;i--) #define rng(i,c,n) for(int i=c;i #define _4aNWZeC ios::sync_with_stdio(0),cin.tie(0) using ll=long long; using pii=pair; using vi=vector; void print(){cout<<'\n';} template void print(const h&v,const t&...u){cout<>n>>m; vi a(n); rep(i,n){ cin>>a[i]; } vi b(n); rep(i,n){ cin>>b[i]; } vi d(n); rep(i,n){ d[i]=(a[i]-b[i]+m)%m; } vi nd; nd.pb(d[0]); rng(i,1,n){ nd.pb((d[i]-d[i-1]+m)%m); } nd.pb((2*m-d.back())%m); d=nd; n+=1; int ans=0; deque dq(d.begin(), d.end()); sort(dq.begin(), dq.end()); while(1){ while(sz(dq) and dq.front()==0) dq.pop_front(); while(sz(dq) and dq.back()==m) dq.pop_back(); if(!sz(dq)) break; assert(sz(dq)>1); int val=min(dq[0],m-dq.back()); dq[0]-=val; dq.back()+=val; ans+=val; } cout<