#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
const int N=200005;
int n,l,a[N],b[N],goal[N],p[N];
ll ans;
vector<int> vec;
map<int,vector<int>> mp;
void mandown(){
	puts("-1");
	exit(0);
}
ll solve(int l,int r){
	if(l+1==r){
		return 0;
	}
	ll res=0;
	int mid=l,pos;
	while(mid+1<r&&b[mid+1]<a[mid+1]){
		mid++;
	}
	for(int i=mid,j=i;i>l;i--,j=min(j,i)){
		vector<int> &tmp=mp[b[i]-i];
		if(tmp.size()==0){
			mandown();
		}
		auto it=lower_bound(tmp.begin(),tmp.end(),j);
		it--;
		if(*it<l){
			mandown();
		}
		goal[i]=*it;
		j=min(j,*it+1);
	}
	for(int i=mid+1,j=i;i<r;i++,j=max(j,i)){
		vector<int> &tmp=mp[b[i]-i];
		auto it=upper_bound(tmp.begin(),tmp.end(),j);
		if(it==tmp.end()||*it>r){
			mandown();
		}
		goal[i]=*it;
		j=max(j,*it-1);
	}
	for(int i=l;i<=r;i++){
		p[i]=1;
	}
	pos=mid;
	for(int i=mid;i>l;i--){
		if(pos!=goal[i]){
			res+=p[pos]+pos-goal[i]-1;
			p[goal[i]]+=p[pos]+pos-goal[i]-2;
			pos=goal[i];
		}else{
			p[pos]--;
		}
	}
	pos=mid+1;
	for(int i=mid+1;i<r;i++){
		if(pos!=goal[i]){
			res+=p[pos]+goal[i]-pos-1;
			p[goal[i]]+=p[pos]+goal[i]-pos-2;
			pos=goal[i];
		}else{
			p[pos]--;
		}
	}
	return res;
}
int main(){
	scanf("%d%d",&n,&l);
	n+=2;
	a[1]=b[1]=0;
	a[n]=b[n]=l+1;
	for(int i=2;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=2;i<n;i++){
		scanf("%d",&b[i]);
	}
	for(int i=1;i<=n;i++){
		if(a[i]==b[i]){
			vec.push_back(i);
		}
		mp[a[i]-i].push_back(i);
	}
	for(int i=0;i<(int)vec.size()-1;i++){
		ans+=solve(vec[i],vec[i+1]);
	}
	printf("%lld\n",ans);
	return 0;
}
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    // 各ペンギンの間のマスに注目
    int N, L;
    cin >> N >> L;
    vector<int> A(N);
    rep(i, N) cin >> A[i];
    A.push_back(L+1);
    vector<int> B(N);
    rep(i, N) cin >> B[i];
    B.push_back(L+1);
    vector<ll> roomA(N+1);
    vector<ll> roomB(N+1);
    roomA[0] = A[0] - 1;
    roomB[0] = B[0] - 1;
    rep(i, N) {
        roomA[i+1] = A[i+1] - 1 - A[i];
        roomB[i+1] = B[i+1] - 1 - B[i];
    }
    ll ans = 0;
    int cur = 0; // roomAの今のindex
    for (int i = 0; i < N+1; i++) {
        // cout << ans << '\n';
        if (roomB[i] == 0) {
            continue;
        }
        ll temp = 0;
        while (cur < N+1 && roomA[cur] == 0) cur++;
        ans += max(0, i - cur);
        while (cur < N+1 && temp < roomB[i]) {
            temp += roomA[cur++];
        }
        if (temp != roomB[i]) {
            cout << -1 << '\n';
            return 0;
        }
        ans += max(0, cur - 1 - i);
    }
    cout << ans << '\n';
    return 0;
    // 境界、ll, 0, -, 1i, for s&g, debug
}