#include<stdio.h>
#include<vector>
#include<algorithm>
#include<deque>
using namespace std;
typedef long long ll;
ll pl[303030], tm[303030];
int main()
{
	int num, len;
	scanf("%d%d", &num, &len);
	for (int i = 0; i < num; i++)scanf("%lld", &pl[i]);
	ll ad = 0;
	vector<ll>v1, v2;
	deque<ll>deq;
	ll maxi = 0, v3 = -1;
	for (int i = 0; i < num; i++)
	{
		ll z;
		scanf("%lld", &z);
		ad += (z - 1) / (len * 2);
		z = (z - 1) % (len * 2) + 1;
		bool f1 = (z <= (len - pl[i]) * 2), f2 = (z <= pl[i] * 2);
		if (f1&&f2)deq.push_back(pl[i]);
		if (f1&&!f2)v1.push_back(pl[i]);
		if (!f1&&f2)v2.push_back(pl[i]);
		if (!f1&&!f2)v3 = pl[i], ad++;
		if (f1 || f2)maxi = pl[i];
	}
	if (!v2.empty())
	{
		ll ans = v1.size() + v2.size() + 1;
		for (int i = 0; i < v1.size(); i++)if (!deq.empty() && deq[0] < v1[i])deq.pop_front();
		v2.push_back(v2[v2.size() - 1]);
		for (int i = v2.size() - 1; i >= 0; i--)
		{
			if (!deq.empty() && v2[i] < deq[deq.size() - 1])deq.pop_back();
			else maxi = len;
		}
		ans += (deq.size() + 1) / 2;
		if (deq.size() % 2 == 1)maxi = len;
		if (maxi < v3)ans++;
		printf("%lld\n", (ad + ans)*len * 2);
	}
	else
	{
		if (!v1.empty())
		{
			for (int i = 0; i < v1.size(); i++)
			{
				if (deq.empty() && v3 < maxi)
				{
					printf("%lld\n", (ad + v1.size())*len * 2);
					return 0;
				}
				if (!deq.empty() && deq[0] < v1[i])deq.pop_front();
			}
			ll ans = v1.size() + 1;
			ans += deq.size() / 2;
			if (deq.size() % 2 == 0)maxi = len;
			if (maxi < v3)ans++;
			printf("%lld\n", (ad + ans)*len * 2);			
		}
		else
		{
			ll ans = 1;
			ans += deq.size() / 2;
			if (deq.size() % 2 == 0)maxi = len;
			if (maxi < v3)ans++;
			printf("%lld\n", (ad + ans)*len * 2);
		}
	}
}