#include<bits/stdc++.h>
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define pii pair<int,int>
#define fi first
#define se second
#define PB push_back
#define ll long long
#define ull unsigned long long
#define y1 orzkcz
using namespace std;
const int N=500005;
int n,L,a[N],b[N],p[N];
int main(){
	scanf("%d%d",&n,&L);
	For(i,1,n) scanf("%d",&a[i]);
	For(i,1,n) scanf("%d",&b[i]);
	a[n+1]=b[n+1]=L+1;
	For(i,1,n+1) a[i]-=i,b[i]-=i;
	For(i,1,n+1){
		p[i]=lower_bound(a,a+n+2,b[i])-a;
		if (a[p[i]]!=b[i]) return puts("-1"),0;
		//cout<<p[i]<<' '; cout<<endl;
	}
	ll ans=0;
	For(i,1,n+1) if (b[i]!=b[i-1]){
		int pp=p[i-1],v=max(i-1-pp,0);
		for (;a[pp+1]==a[pp];++pp) v=min(v,max(0,i-1-(pp+1)));
		ans+=v+max(p[i]-i,0);
	}
	cout<<ans<<endl;
}
/*input
10 1000000000
65110170 68805223 123016442 275946481 661490312 760727752 764540566 929355340 930658577 947099792
1 2 123016442 661490311 929355337 930658574 999999997 999999998 999999999 1000000000

*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define eb emplace_back
#define pb push_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (int)(lower_bound(c.begin(),c.end(),x)-c.begin())
#define EL cout<<'\n'
#define BS(a,x) binary_search(ALL(a),x)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
	out<<'('<<P.F<<','<<P.S<<')';
	return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
	REP(i,SZ(V)) out<<V[i]<<((i!=SZ(V)-1)?" ":"");
	return out;
}
//}}}
const ll maxn=300005;
const ll maxlg=20;
const ll INF64=1e18;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const ld PI=acos(-1);
const ld eps=1e-9;
//const ll p=880301;
//const ll P=31;

ll mypow(ll a,ll b){
	ll res=1LL;
	while(b){
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;
		b>>=1;
	}
	return res;
}

ll a[maxn],b[maxn];

vector<ll> A, B;

ll ans = 0;
int main(){
	IOS;
	int n;
	ll L;
	cin>>n>>L;
	REP(i,n) cin>>a[i+1];
	REP(i,n) cin>>b[i+1];
	a[n+1] = b[n+1] = L+1;

	for(int i=0;i<n+1;i++){
		A.pb(a[i+1]-a[i]-1);
	}
	for(int i=0;i<n+1;i++){
		B.pb(b[i+1]-b[i]-1);
	}
	// cout<<A<<'\n';
	// cout<<B<<'\n';	
	int pt = 0;

	REP(i,SZ(B)) if(B[i]){
		ll left = B[i];
		int first_non_zero = -1;
		while(pt<SZ(A)&&left>0){
			left -= A[pt];
			if(A[pt]!=0&& first_non_zero == -1) first_non_zero = pt;
			pt++;
		}
		if(left<0){
			cout<<"-1\n";
			return 0;
		}
		if(first_non_zero <= i && i <= pt-1){
			ans+=pt-1-first_non_zero;
		}
		else if(first_non_zero > i){
			ans += pt-1-i;
		}
		else ans += i-first_non_zero;
		// cout<<i<<' '<<first_non_zero<<' '<<pt-1<<'\n';
	}
	cout<<ans<<'\n';

	return 0;
}
