#include <bits/stdc++.h>

using namespace std;

#define FOE(i, s, t) for (int i = s; i <= t; i++)
#define FOR(i, s, t) for (int i = s; i < t; i++)
#define FOD(i, s, t) for (int i = s; i >= t; i--)

#define LL long long
#define mp make_pair
#define pb push_back

#define K 600001

int a[K], b[K], n, l;
int c[K], d[K];

vector<int> v;

int main() {
	scanf("%d%d", &n, &l);
	FOE(i, 1, n) scanf("%d", &a[i]);
	FOE(i, 1, n) scanf("%d", &b[i]);

	FOE(i, 1, n) {
		c[i] = a[i] - (i > 1 ? a[i - 1] : 0) - 1;
		d[i] = b[i] - (i > 1 ? b[i - 1] : 0) - 1;	
	}
	
	c[n + 1] = l - a[n];
	d[n + 1] = l - b[n];
	
	int ptr = 1;
	LL tot = 0;
	LL ret = 0;
	
	while (ptr <= n + 1 && d[ptr] == 0) ptr++;
	
	FOE(i, 1, n + 1) {
		tot += c[i];
		if (c[i]) v.pb(i);
		
		if (tot > d[ptr]) {
			puts("-1");
			return 0;
		}
		
		if (ptr <= n + 1 && tot == d[ptr]) {
			int l = v[0];
			int r = v[v.size() - 1];
			
			if (l < ptr) ret += (ptr - l);
			if (ptr < r) ret += (r - ptr);
			
			ptr++;
			while (ptr <= n + 1 && d[ptr] == 0) ptr++;
			tot = 0;	
			
			v.clear();
		}
	}
	
	if (v.size() >= 1) {
		puts("-1");	
	} else {
		printf("%lld\n", ret);	
	}
}
#include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
#include <utility>


using namespace std;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for(int i = 0;i < (n);i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=1000000007;
const llint inf=2.19e15+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-10;
template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}

int main(void){
	int i,j,n,L;cin>>n>>L;
	llint ans=0;
	vector<int>A(n+2);
	vector<int>B(n+2);
	for(i=1;i<=n;i++){cin>>A[i];A[i]-=i;}
	for(i=1;i<=n;i++){cin>>B[i];B[i]-=i;}
	A[n+1]=L-n;B[n+1]=L-n;
	for(i=1;i<=n;i++){
		if(A[i]<B[i]){
			int bas=LBI(A,B[i]);
			if(A[bas]!=B[i]){cout<<-1<<endl;return 0;}
			if(B[i]==B[i+1]){ans++;}
			else{ans+=bas-i;}
		}
		if(A[i]>B[i]){
			int bas=UBI(A,B[i])-1;
			if(A[bas]!=B[i]){cout<<-1<<endl;return 0;}
			if(B[i]==B[i-1]){ans++;}
			else{ans+=i-bas;}
		}
		//cerr<<"ok";
	}
	cout<<ans<<endl;
	return 0;
}