#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define x first
#define y second

using namespace std;

typedef long long ll;

int start[100005], finish[100005];

int main()
{
 	//freopen("input.txt", "r", stdin);
 	//freopen("output.txt", "w", stdout);
 	ios_base::sync_with_stdio(0); cin.tie(0);
 	int n, len;
 	cin >> n >> len;
 	for (int i = 1; i <= n; i++) cin >> start[i];
 	for (int i = 1; i <= n; i++) cin >> finish[i];
 	start[n + 1] = len + 1, finish[n + 1] = len + 1, start[0] = 0, finish[0] = 0;

	queue <int> qu;
	ll ans = 0;
	for (int i = 0; i <= n + 1; i++)
	{
	 	if (start[i] > finish[i] && !qu.empty()) cout << "-1\n", exit(0);
	 	if (!qu.empty() && start[i] - finish[qu.front()] == i - qu.front()) ans += (ll) qu.size();
	 	while (!qu.empty() && start[i] - finish[qu.front()] == i - qu.front()) qu.pop();
	 	if (start[i] == finish[i] && !qu.empty()) cout << "-1\n", exit(0);
	 	if (i == n + 1) break;
		if (start[i] < finish[i]) qu.push(i);
	}
	for (int i = n + 1; i >= 0; i--)
	{
	 	if (start[i] < finish[i] && !qu.empty()) cout << "-1\n", exit(0);
	 	if (!qu.empty() && finish[qu.front()] - start[i] == qu.front() - i) ans += (ll) qu.size();
	 	while (!qu.empty() && finish[qu.front()] - start[i] == qu.front() - i) qu.pop();
	 	if (start[i] == finish[i] && !qu.empty()) cout << "-1\n", exit(0);
	 	if (i == 0) break;
        if (start[i] > finish[i]) qu.push(i);
	}
	cout << ans << '\n';
 	return 0;
}
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define NDEBUG
#define eprintf(...) do {} while (0)
#endif
#include<cassert>

using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

bool yes;
LL X[100011], Y[100011];
LL solve(int len) {
    if (len <= 1) return 0;
//    rprintf("%lld", X, X+len);
//    rprintf("%lld", Y, Y+len);

    LL ret = len - 1;
    int left = 0;
    for (int i=1; i<len; ) {
	int j = i;
	while (j < len && Y[i] == Y[j]) j++;
	while (X[left] <= Y[i]) left++;

	if (left && X[left-1] == Y[i]) {
	    ret += i - left;
	} else {
	    yes = false;
	    return 0;
	}

	i = j;
    }

    return ret;
}

int N;
LL L;
LL A[100011];
LL B[100011];



void MAIN() {
    scanf("%d%lld", &N, &L);
    A[0] = B[0] = 0;
    REP (i, N) scanf("%lld", A+i+1);
    REP (i, N) scanf("%lld", B+i+1);
    N += 2;
    A[N-1] = B[N-1] = L+1;

    REP (i, N) {
	A[i] -= i;
	B[i] -= i;
    }

    yes = true;
    LL ans = 0;
    for (int i=0; i<N && yes; ) {
	int j = i + 1;
	while (B[j] < A[j]) j++;

	int len = j - i;
	REP (a, len) {
	    X[a] = A[i+a] - A[i];
	    Y[a] = B[i+a] - B[i];
	}
	ans += solve(len);

	int k = j;
	while (A[k] < B[k]) k++;
	if (B[k] < A[k]) {
	    yes = false;
	    break;
	}

	len = k - j + 1;
	REP (a, len) {
	    X[a] = A[k] - A[k-a];
	    Y[a] = B[k] - B[k-a];
	}
	ans += solve(len);

	i = k;
    }

    if (yes) {
	printf("%lld\n", ans);
    } else {
	puts("-1");
    }
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

