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;

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}

int nextInt() { int x; scanf("%d", &x); return x;}

const int MAX_N = 1005;
const int MAX_K = MAX_N;
int dp[MAX_N][MAX_K];

int main() {

  for (;;) {
    int N = nextInt();
    ll MOD = nextInt();
    if (N == 0) break;
    vector<int> a(N), b(N), c(N);
    REP(i, N) a[i] = nextInt();
    REP(i, N) b[i] = nextInt();
    REP(i, N) c[i] = (b[i] - a[i] + MOD) % MOD;

    const int INF = 1001001001;
    REP(i, MAX_N) REP(j, MAX_K) dp[i][j] = INF;

    dp[0][0] = c[0];
    for (int i = 1; i < N; i++) {
      for (int j1 = 0; j1 < MAX_K; j1++) {
        if (dp[i-1][j1] >= INF) continue;
        int ac1 = c[i-1] + j1 * MOD;
        for (int j2 : {j1-1, j1, j1+1}) if (0 <= j2 && j2 < MAX_K) {
          int ac2 = c[i] + j2 * MOD;
          int cost = max(0, ac2 - ac1);
          chmin(dp[i][j2], dp[i-1][j1] + cost);
        }
      }
    }
    int ans = dp[N-1][0];
    REP(j, MAX_K) chmin(ans, dp[N-1][j]);
    cout << ans << endl;
  }
  return 0;
}