#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <random>
#include <chrono>
#include <tuple>
#include <random>
#include <cmath>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

const ll SZ = 2e5 + 100, INF = 1e9 * 1e9 + 10, MOD = 998244353;
vector<ll> vec1, vec2;
vector<pair<ll, ll>> v1, v2;

ll fnd(int j) {
	int l = 0, r = v1.size();
	while (r - l > 1) {
		int mid = (l + r) / 2;
		int newps = v1[mid].first - abs(j - mid);
		if (newps >= v1[j].second) {
			r = mid;
		}
		else {
			l = mid;
		}
	}

	return r;
}

ll get() {
	vector<ll> c1, c2;
	v1.clear();

	for (int i = 0; i < vec1.size() - 1; i++) {
		ll dir = 0, dir2 = 0;
		if (vec1[i] < vec2[i]) dir = 1;
		if (vec1[i] > vec2[i]) dir = 2;

		if (vec1[i + 1] < vec2[i + 1]) dir2 = 1;
		if (vec1[i + 1] > vec2[i + 1]) dir2 = 2;

		if (dir == 1 || dir == 0) {
			v1.push_back({ vec1[i], vec2[i] });
		}

		if (dir == 1 && dir2 == 2) {
			cout << "-1";
			exit(0);
		}
	}

	
	v1.push_back({ vec1.back(), vec2.back() });
	ll ans = 0;
	
	ll upd = -1;

	for (int i = 0; i < v1.size(); i++) {
		if (upd != -1) {
			v1[i].first = v1[upd].first - abs(upd - i);
			if (upd == i) upd = -1;
		}
		if (v1[i].first == v1[i].second) continue;

		auto it = lower_bound(v1.begin(), v1.end(), v1[fnd(i)]);

		if (upd == -1 || (it - v1.begin()) >= upd) {
			upd = (it - v1.begin());
			if (v1[i].second != v1[(it - v1.begin())].first - abs(i - (it - v1.begin()))) {
				cout << "-1";
				exit(0);
			}

			ans += abs(i - (it - v1.begin()));
		}
		else {
			ll j = (it - v1.begin());

			v1[j].first = v1[upd].first - abs(upd - j);
			if (v1[i].second != v1[(it - v1.begin())].first - abs(i - (it - v1.begin()))) {
				cout << "-1";
				exit(0);
			}

			ans += abs(i - (it - v1.begin()));
		}
	}

	return ans;
}

int main()
{
	fastInp;

	ll n, l;
	cin >> n >> l;

	vec1.resize(n);
	vec2.resize(n);

	for (auto &cur : vec1) cin >> cur;
	for (auto &cur : vec2) cin >> cur;
	vec1.insert(vec1.begin(), 0);
	vec2.insert(vec2.begin(), 0);
	vec1.push_back(l + 1);
	vec2.push_back(l + 1);

	
	ll q = get();

	for (int i = 0; i < vec1.size(); i++) {
		vec1[i] = l + 1 - vec1[i];
		vec2[i] = l + 1 - vec2[i];
	}

	reverse(vec1.begin(), vec1.end());
	reverse(vec2.begin(), vec2.end());

	ll k = q + get();
	
	cout << k;

	return 0;
}
#include <bits/stdc++.h>
#define fo(a,b,c) for (a=b; a<=c; a++)
#define fd(a,b,c) for (a=b; a>=c; a--)
#define min(a,b) (a<b?a:b)
#define ll long long
//#define file
using namespace std;

int a[100002],b[100002],n,L,i,j,k,l,sum;
ll ans;

void Exit() {printf("-1\n");exit(0);}

int main()
{
	#ifdef file
	freopen("c.in","r",stdin);
	#endif
	
	scanf("%d%d",&n,&L);
	fo(i,1,n) scanf("%d",&a[i]);
	fo(i,1,n) scanf("%d",&b[i]);
	
	a[0]=0,a[n+1]=L+1;
	b[0]=0,b[n+1]=L+1;
	l=0;
	fo(i,0,n)
	if (b[i]+1<b[i+1])
	{
		sum=0;
		while (l<=n && a[l]+1==a[l+1]) ++l;
		k=min(l,i);
		
		while (l<=n && sum<b[i+1]-b[i]-1)
		sum+=a[l+1]-a[l]-1,++l;
		
		if (sum>b[i+1]-b[i]-1) Exit();
		ans+=max(l,i+1)-k-1;
	}
	printf("%lld\n",ans);
	
	fclose(stdin);
	fclose(stdout);
	return 0;
}