#include using namespace std; int main(){ int N, M; cin >> N >> M; vector A(N); for (int i = 0; i < N; i++){ cin >> A[i]; } vector B(N); for (int i = 0; i < N; i++){ cin >> B[i]; } vector D(N + 2); D[0] = 0; D[N + 1] = 0; for (int i = 0; i < N; i++){ D[i + 1] = (B[i] - A[i] + M) % M; } vector DD(N + 1); for (int i = 0; i <= N; i++){ DD[i] = (D[i + 1] - D[i] + M) % M; } long long sum = 0; for (int i = 0; i <= N; i++){ sum += DD[i]; } int cnt = sum / M; sort(DD.begin(), DD.end(), greater()); for (int i = 0; i < cnt; i++){ DD[i] -= M; } long long ans = 0; for (int i = 0; i <= N; i++){ ans += abs(DD[i]); } cout << ans / 2 << endl; }