#define ATCODER #define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; using ld = long double; using pll = pair; using pdd = pair; //template using pq = priority_queue, greater>; #define FOR(i, a, b) for(ll i=(a); i<(b);i++) #define REP(i, n) for(ll i=0; i<(n);i++) #define ROF(i, a, b) for(ll i=(b-1); i>=(a);i--) #define PER(i, n) for(ll i=n-1; i>=0;i--) #define REPREP(i,j,a,b) for(ll i=0;i > #define VV2(type,n,m,val) vector< vector > val;val.resize(n);for(ll i;i #define VEC(type,n,val) vector val;val.resize(n) #define VL vector #define VVL vector< vector > #define VP vector< pair > #define SZ size() #define all(i) begin(i),end(i) #define SORT(i) sort(all(i)) #define BITI(i) (1<>i) & 1) != 0) #define ALLBIT(n) (ll(1)<v?v:n #define MP(a,b) make_pair(a,b) #define DET2(x1,y1,x2,y2) x1*y2-x2*y1 #define DET3(x1,y1,z1,x2,y2,z2,x3,y3,z3) x1*y2*z3+x2*y3*z1+x3*y1*z2-z1*y2*x3-z2*y3*x1-z3*y1*x2 #define INC(a) for(auto& v:a)v++; #define DEC(a) for(auto& v:a)v--; #define SQU(x) (x)*(x) #define L0 ll(0) #ifdef ATCODER #include using namespace atcoder; using mint = modint1000000007; using mint2 = modint998244353; #endif template vector read(size_t n) { vector ts(n); for (size_t i = 0; i < n; i++) cin >> ts[i]; return ts; } template void read_tuple_impl(TV&) {} template void read_tuple_impl(TV& ts) { get(ts).emplace_back(*(istream_iterator(cin))); read_tuple_impl(ts); } template decltype(auto) read_tuple(size_t n) { tuple...> ts; for (size_t i = 0; i < n; i++) read_tuple_impl(ts); return ts; } using val = pair; using func = ll; val op(val a, val b) { return MP(a.first + b.first, a.second + b.second); } val e() { return MP(0, 0); } val mp(func f, val a) { return MP(a.first + f * a.second, a.second); } func comp(func f, func g) { return f + g; } func id() { return 0; } ll di[4] = { 1,0,-1,0 }; ll dj[4] = { 0,1,0,-1 }; ll si[4] = { 0,3,3,0 }; ll sj[4] = { 0,0,3,3 }; //ll di[4] = { -1,-1,1,1 }; //ll dj[4] = { -1,1,-1,1 }; ll di8[8] = { 0,-1,-1,-1,0,1,1,1 }; ll dj8[8] = { -1,-1,0,1,1,1,0,-1 }; void solve() { ll n, m; cin >> n >> m; VL a = read(n); VL b = read(n); const int refC = 64; vector dp(n, vector(refC*2, 1e18)); REP(i, refC) { dp[0][i] = (a[0] + m - b[0]) % m + i * m; dp[0][i + refC] = (b[0] + m - a[0]) % m + i * m; } REP(i, n - 1) { VL bf(refC*2), af(refC*2); REP(j, refC) { bf[j] = (a[i] + m - b[i]) % m + j * m; bf[j + refC] = (b[i] + m - a[i]) % m + j * m; af[j] = (a[i+1] + m - b[i+1]) % m + j * m; af[j + refC] = (b[i+1] + m - a[i+1]) % m + j * m; } REP(i1, refC*2) { REP(i2, refC*2) { if (i1 / refC == i2 / refC) { ll tmp = dp[i][i2] + max(af[i1] - bf[i2], 0LL); if (dp[i + 1][i1] > tmp) { dp[i + 1][i1] = tmp; } } else { ll tmp = dp[i][i2] + af[i1]; if (dp[i + 1][i1] > tmp) { dp[i + 1][i1] = tmp; } } } } } cout << *min_element(all(dp[n - 1])); return; } int main() { ll t = 1; //cin >> t; while (t--) { solve(); } return 0; }