#include <bits/stdc++.h>
using namespace std;
/*
#include <atcoder/all>
using namespace atcoder;
*/

#define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i)
#define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i)
#define REP(i, n) rep(i, 0, n)
#define REP2(i, n) rep2(i, 0, n)
#define all(hoge) (hoge).begin(), (hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T>
using vec = vector<T>;
template <class T>
using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
using tp = tuple<ll, ll, ll>;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll)1e9 + 7;
//constexpr long long MOD = 998244353LL;
using ld = long double;
static const ld pi = 3.141592653589793L;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

template <class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

//グラフ関連
struct Edge {
    ll to, cap, rev;
    Edge(ll _to, ll _cap, ll _rev) {
        to = _to;
        cap = _cap;
        rev = _rev;
    }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
    G[from].push_back(Edge(to, cap, (ll)G[to].size()));
    if(revFlag)
        G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void solve() {
    ll n, l;
    cin >> n >> l;
    vec<ll> a(n), b(n);
    REP(i, n) {
        cin >> a[i];
    }
    REP(i, n) {
        cin >> b[i];
    }

    vec<ll> can(n, false);

    map<ll, ll> mp;
    mp[1] = -1; //ai-i=0-(-1)=1
    ll ans = 0;
    REP(i, n) {
        if(a[i] < b[i]) {
            mp.clear();
            mp[a[i] - i] = i;
            continue;
        }
        if(a[i] == b[i]) {
            can[i] = true;
            mp.clear();
            mp[a[i] - i] = i;
        } else if(i > 0 and b[i - 1] + 1 == b[i]) {
            can[i] = true;
            ans++;
            mp[a[i] - i] = i;
        } else if(mp.count(b[i] - i)) {
            can[i] = true;
            ans += i - mp[b[i] - i];
            mp[a[i] - i] = i;
        }
    }
    mp.clear();
    mp[l + 1 - n] = n; //l+1-n
    REP2(i, n) {
        if(can[i]) {
            //cout << "can" << i << en;
            mp.clear();
            mp[a[i] - i] = i;
            continue;
        }

        if(i + 1 < n and b[i + 1] - 1 == b[i]) {
            can[i] = true;
            ans++;
            mp[a[i] - i] = i;
        } else if(mp.count(b[i] - i)) {
            can[i] = true;
            ans += mp[b[i] - i] - i;
            mp[a[i] - i] = i;
        } else {
            //cout << i << " " << b[i] - i << en;
            cout << -1 << en;
            return;
        }
        //cout << a[i] - i << en;
    }
    cout << ans << en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    /*
    ll t;
    cin >> t;
    while(t--)*/
    solve();

    return 0;
}
#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;
}