#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <climits>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <map>
#include <tuple>
#include <iostream>
#include <deque>
#include <array>
#include <set>
#include <functional>
#include <memory>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <numeric>
int solve(const int max, const std::vector<int>& counter, const std::vector<int>& target, const int counter_pos, const int overflow, std::vector<std::vector<int>>& memo) {
	if (counter_pos == 0) {
		return solve(max, counter, target, counter_pos + 1, 0, memo) + (target[0] - counter[0] + max) % max;
	}
	else if (counter_pos == counter.size()) {
		return 0;
	}
	else if (memo[counter_pos][overflow] >= 0) {
		return memo[counter_pos][overflow];
	}
	else {
		auto prev = (target[counter_pos - 1] - counter[counter_pos - 1] + max) % max;
		auto temp = (counter[counter_pos] + prev) % max;
		memo[counter_pos][overflow] = solve(max, counter, target, counter_pos + 1, (prev + (target[counter_pos] - temp + max) % max >= max) ? overflow + 1 : overflow, memo) + (target[counter_pos] - temp + max) % max;
		if ((target[counter_pos] - counter[counter_pos] + max) % max <= prev) {
			memo[counter_pos][overflow] = std::min(memo[counter_pos][overflow], solve(max, counter, target, counter_pos + 1, overflow, memo));
		}
		if (overflow > 0) {
			memo[counter_pos][overflow] = std::min(memo[counter_pos][overflow], solve(max, counter, target, counter_pos + 1, overflow - 1, memo));
		}
		return memo[counter_pos][overflow];
	}
}
int main() {
	while (true) {
		int n, m; std::cin >> n >> m;
		if (n == 0) break;
		std::vector<int>counter(n); for (auto& a : counter) std::cin >> a;
		std::vector<int>target(n); for (auto& b : target) std::cin >> b;
		std::vector<std::vector<int>> memo(n, std::vector<int>(n, -1));
		std::cout << solve(m, counter, target, 0, 0, memo) << std::endl;
	}
}
