Problem: <!--

Problem D

-->

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.

<image> 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






Example

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

4
0
14731

Solution (in CPP):

Solution:
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<int,int> P;
const LL mod=1000000007;
const LL LINF=1LL<<60;
const int INF=1<<30;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};



int main(){
    int n,m;
    while(cin >> n >> m,n){
        vector<int> a(n+1,0),b(n+1,0);
        for (int i = 0; i < n; i++) {
            cin >> a[i+1];
        }
        for (int i = 0; i < n; i++) {
            cin >> b[i+1];
        }
        vector<vector<int>> dp(n+1,vector<int> (n+3,INF));
        dp[0][0]=0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if(b[i+1]+m*(j+1)-a[i+1]>b[i]+m*j-a[i]) dp[i+1][j+1] = min(dp[i+1][j+1],dp[i][j]+b[i+1]+(j+1)*m-a[i+1]-(b[i]+m*j-a[i]));
                else dp[i+1][j+1] = min(dp[i+1][j+1],dp[i][j]);

                if(b[i+1]+m*(j+2)-a[i+1]>b[i]+m*j-a[i]) dp[i+1][j+2] = min(dp[i+1][j+2],dp[i][j]+b[i+1]+(j+2)*m-a[i+1]-(b[i]+m*j-a[i]));
                else dp[i+1][j+2] = min(dp[i+1][j+2],dp[i][j]);

                if(j&&a[i+1]<=b[i+1]+m*(j-1)){
                    if(b[i+1]+m*(j-1)-a[i+1]>b[i]+m*j-a[i]) dp[i+1][j-1] = min(dp[i+1][j-1],dp[i][j]+b[i+1]+(j-1)*m-a[i+1]-(b[i]+m*j-a[i]));
                    else dp[i+1][j-1] = min(dp[i+1][j-1],dp[i][j]);
                }

                if(j-1&&a[i+1]<=b[i+1]+m*(j-2)){
                    if(b[i+1]+m*(j-2)-a[i+1]>b[i]+m*j-a[i]) dp[i+1][j-2] = min(dp[i+1][j-2],dp[i][j]+b[i+1]+(j-2)*m-a[i+1]-(b[i]+m*j-a[i]));
                    else dp[i+1][j-2] = min(dp[i+1][j-2],dp[i][j]);
                }
                if(a[i+1]<=b[i+1]+m*j){
                    if(b[i+1]+m*j-a[i+1]>b[i]+m*j-a[i]) dp[i+1][j] = min(dp[i+1][j],dp[i][j]+b[i+1]+m*j-a[i+1]-(b[i]+m*j-a[i]));
                    else dp[i+1][j] = min(dp[i+1][j],dp[i][j]);
                }
            }
        }
        int ans=INF;
        for (int i = 0; i < n+2; i++) {
            ans = min(ans,dp[n][i]);
        }
        cout << ans << endl;
    }
    return 0;
}