#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
template<class T> bool chmin(T& a,T b){if(a>b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a,T b){if(a<b) {a = b; return true;} return false;}
#define all(x) (x).begin(),(x).end()
#define debug(x)  cerr << #x << " = " << (x) << endl;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N,L;
    cin >> N >> L;
    vec<int> A(N+2),B(N+2);
    for(int i=1;i<=N;i++) cin >> A[i];
    for(int i=1;i<=N;i++) cin >> B[i];
    A[N+1] = B[N+1] = L+1;
    vec<int> C(N+1),D(N+1);
    for(int i=0;i<=N;i++){
        C[i] = A[i+1]-A[i]-1;
        D[i] = B[i+1]-B[i]-1;
    }
    ll ans = 0;
    int sum = 0,r = 0;
    for(int i=0;i<=N;i++) if(D[i]){
        while(C[r]==0) r++;
        int l = r;
        while(r<=N && sum+C[r]<=D[i]){
            sum += C[r++];
            if(sum==D[i]) break;
        }
        if(sum!=D[i]){
            cout << "-1\n";
            return 0;
        }
        if(r<=i) ans += i-l;
        else if(i<=l) ans += r-i-1;
        else ans += r-l-1;
        sum = 0;
    }
    cout << ans << "\n";
}
#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();
	}
}
