Problem:
Tally Counters

A number of tally counters are placed in a row.
Pushing the button on a counter will increment the displayed value by one, or, when the value is already the maximum, it goes down to one.
All the counters are of the same model with the same maximum value.

Fig. D-1 Tally Counters

Starting from the values initially displayed on each of the counters, you want to change all the displayed values to target values specified for each.
As you don't want the hassle, however, of pushing buttons of many counters one be one, you devised a special tool.
Using the tool, you can push buttons of one or more
adjacent counters, one push for each, in a single operation.
You can choose an arbitrary number of counters at any position in
each operation, as far as they are consecutively lined up.

How many operations are required at least to change the displayed values on counters to the target values?

Input

The input consists of multiple datasets, each in the following format.

n m

a1 a2 ... an

b1 b2 ... bn

Each dataset consists of 3 lines.
The first line contains n (1 ≤ n ≤ 1000) and m (1 ≤ m ≤ 10000), the number of counters and the maximum value displayed on counters, respectively.
The second line contains the initial values on counters, ai (1 ≤ ai ≤ m), separated by spaces.
The third line contains the target values on counters, bi (1 ≤ bi ≤ m), separated by spaces.

The end of the input is indicated by a line containing two zeros.
The number of datasets does not exceed 100.

Output

For each dataset, print in a line the minimum number of operations required to make all of the counters display the target values.

Sample Input

4 5
2 3 1 4
2 5 5 2
3 100
1 10 100
1 10 100
5 10000
4971 7482 1238 8523 1823
3287 9013 9812 297 1809
0 0

Output for the Sample Input

4
0
14731

Solution in C++14 (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
int PREP = (cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(9), 0);
//int SEGV = getenv("D") || (exit(system("D= SEGFAULT_SIGNALS=all catchsegv ./prog.exe") >> 8), 0);
Int dp[1010][1010];
template <class T> void chmin(T& x, T y) { if (x > y) x = y; }
int main() {
    for (;;) {
        Int N, M; cin >> N >> M;
        if (N == 0) break;
        vector<Int> A(N); for (auto &a : A) cin >> a;
        vector<Int> B(N); for (auto &b : B) cin >> b;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N + 1; j++) {
                dp[i][j] = 1e18;
            }
        }
        for (int j = 0; j < N + 1; j++) {
            if (B[0] + M * j >= A[0]) {
                dp[0][j] = B[0] + M * j - A[0];
            }
        }
        for (int i = 1; i < N; i++) {
            for (int j = 0; j <= N; j++) {
                for (int k = max(0, j - 2); k <= min((int)N, j + 2); k++) {
                    if (B[i] + M * j >= A[i]) {
                        Int cost = max(0LL, (B[i] + M * j - A[i]) - (B[i - 1] + M * k - A[i - 1]));
                        chmin(dp[i][j], dp[i - 1][k] + cost);
                    }
                }
            }
        }
        Int ans = 1e18;
        for (int j = 0; j <= N; j++) {
            ans = min(ans, dp[N - 1][j]);
        }
        cout << ans << '\n';
    }
    return 0;
}