#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds;
using namespace std;
typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update> oset;
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
using ll = long long;
#define ar array 
clock_t time_req = clock()*1.0/CLOCKS_PER_SEC;


map<int,pair<int,int>> read(int n , int L){
	vector<int> v{0};
	for(int i = 1 ; i <= n + 1; i++){
		int ai = L+1;
		if(i <= n){
			cin >> ai;
		}
		v.push_back(ai-i);
	}
	map<int,pair<int,int>> interval;
	for(int i = 0 ; i < (int)v.size() ; i++){
		if( i == 0 || v[i] != v[i-1]){
			interval[v[i]].first = i;
		}
		interval[v[i]].second = i;
	}
	return interval;
}

void test_case() {
	int n , L;
	cin >> n >> L;
	auto a = read(n,L);
	auto b = read(n,L);
	debug() << imie(a);
	debug() << imie(b);
	ll answer = 0;
	for(auto pp : b){
		int value = pp.first;
		if(!a.count(value)){
			puts("-1");
			return;
		}
		answer += max(0,a[value].first - b[value].first) + max(0,b[value].second - a[value].second);
	}
	cout<<answer<<"\n";
}
 
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int T = 1;
	//cin >> T;
	while(T--){
		test_case();
	}
}

#include<bits/stdc++.h>

typedef long long ll;
typedef long double ld;
using namespace std;
mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());
//mt19937_64 mrand(42);
#define ii for(int i=1;i<=n;++i)
#define ji for(int j=1;j<=n;++j)
#define jj for(int j=1;j<=m;++j)
#define ij for(int i=1;i<=m;++i)
#define sz(x) ((ll)x.size())
#define all(x) x.begin(),x.end()
#define al(x) x+1,x+1+n
#define asd cout<<"ok"<<endl;
#define asdd cout<<"okok"<<endl;
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vii vector<pair<int,int>>
#define pr(v) for(auto i:v) cout<<i<<" ";cout<<endl;
#define prt(a, l, r) for(auto i=l;i<=r;++i) cout<<a[i]<<" ";cout<<endl;
#define pc(x) __builtin_popcount(x)
#define pb push_back
#define PS string qqwwee;cin>>qqwwee;
typedef pair<int,int> pii;

int a[100005], b[100005];
ll solve(int l, int r, int L, int R) {
    if(l > r) return 0;
    int left = 0, right = 0;
    for(int i=l;i<=r;++i) {
        if(b[i] < a[i]) left = i;
        else if(a[i] < b[i]) {
            if(!right) right = i;
        } else assert(0);
    }
    if(left && right) {
        if(left > right) return -1;
        assert(left + 1 == right);
        ll tmp1 = solve(l, left, L, a[left]+1);
        ll tmp2 = solve(right, r, a[left], R);
        if(tmp1 == -1 || tmp2 == -1) return -1;
        return tmp1 + tmp2;
    }
    if(right) {
        for(int i=l;i<=r;++i) a[i]=-a[i],b[i]=-b[i];
        reverse(a+l,a+r+1);
        reverse(b+l,b+r+1);
        L=-R;
    }
    unordered_map<int,int> mp;
    ll ret=0;
    mp[L-(l-1)]=l-1;
    for(int i=l;i<=r;++i) {
        if(!mp.count(b[i]-i)) return -1;
        ret+=i-mp[b[i]-i];
        mp[b[i]-i]=mp[a[i]-i]=i;
    }
    return ret;
}
int main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n, l;
    cin >> n >> l;
    for(int i=1;i<=n;++i) cin >> a[i];
    for(int i=1;i<=n;++i) cin >> b[i];
    ll ans = 0, lst = 1, lstl = 0;
    for(int i=1;i<=n;++i) {
        if(a[i] == b[i]) {
            ll tmp = solve(lst, i-1, lstl, b[i]);
            if(tmp < 0) {
                ans = -1;
                break;
            }
            ans += tmp;
            lst = i+1;
            lstl = a[i];
        }
    }
    if(ans != -1) {
        ll tmp = solve(lst, n, lstl, l+1);
        if(tmp < 0) ans = -1;
        else ans += tmp;
    }
    cout << ans << endl;
}