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

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define getchar_unlocked getchar

int A[100005];
int B[100005];
int offset[100005];
int minLevel1[100005];
int minLevel2[100005];
int N, L;

inline int nextInt(){
    int x=0;
    char ch=0;
    do{
        ch=getchar_unlocked();
    }while(ch<'0'||ch>'9');
    while(ch>='0'&&ch<='9'){
        x=10*x+(ch-'0');
        ch=getchar_unlocked();
    }
    return x;
}

int main(){
    N = nextInt();
    L = nextInt();
    for(int i = 1; i <= N; i ++){
        A[i] = nextInt();
        offset[i] = A[i]-i;
    }
    for(int i = 1; i <= N; i ++){
        B[i] = nextInt();
    }

    for(int i = 1; i <= N; i ++){
        minLevel1[i] = 0;
        minLevel2[i] = 0;
        //printf("offset[%d]=%d\n", i, offset[i]);
    }

    long long ans = 0;
    int lo = 1;
    int hi2 = 1;
    for(int i = 1; i <= N; i ++){
        if(A[i] == B[i]){continue;}
        if(B[i] == i || B[i] == L-N+i){
            ans ++;
        }else{
            while(lo < N+2 && offset[lo] < B[i]-i){
                lo ++;
            }

            if((lo == N+2) || (offset[lo] != B[i]-i)){
                printf("-1");
                return 0;
            }

            hi2 = max(hi2, lo);
            while(hi2 < N+2 && offset[hi2] <= B[i]-i){
                hi2 ++;
            }

            int hi = hi2-1;
            //int hi = upper_bound(offset+1, offset+(1+N), B[i]-i) - offset - 1;

            if(lo > i){
                minLevel2[lo] = max(minLevel2[lo], lo - i);
            }else{
                minLevel1[hi] = max(minLevel1[hi], i - hi);
            }

            //printf("i=%d [%d, %d]\n", i, lo, hi);
            //ans += min(abs(lo - i), abs(hi - i));
        }
    }

    for(int i = 1; i <= N; i ++){
        ans += minLevel1[i] + minLevel2[i];
    }

    printf("%lld\n", ans);
    return 0;
}

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

template <typename T>
void print(const vector<T>& a, const string& name = "") {
	if (!name.empty()) cout << name << ": ";
	for (const auto& item : a) {
		cout << item << " ";
	}
	cout << endl;
}

template <typename T1, typename T2>
void print(const pair<T1, T2>& p, const string& name = "") {
	if (!name.empty()) cout << name << ": ";
	cout << "(" << p.first << ", " << p.second << ") " << endl;
}

template <typename T1, typename T2>
void print(const vector<pair<T1, T2>>& pairs, const string& name = "") {
	if (!name.empty()) cout << name << ": ";
	for (const auto& pair : pairs) {
		cout << "(" << pair.first << ", " << pair.second << ") ";
	}
	cout << endl;
}
template <typename T1, typename T2>
void print(const map<T1, T2>& m, const string& name = "") {
	if (!name.empty()) cout << name << ": ";
	for (const auto& p : m) {
		cout << "(" << p.first << ", " << p.second << ") ";
	}
	cout << endl;
}

int main() {
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int N; cin >> N;
	// you can attain 0..L-N
	int L; cin >> L; L -= N;
 
	vector<int> A; A.reserve(N+2);
	A.push_back(0);
	for (int i = 0; i < N; i++) {
		int a; cin >> a; a--;
		A.push_back(a-i);
	}
	A.push_back(L);
 
	vector<int> B; B.reserve(N+2);
	B.push_back(0);
	for (int i = 0; i < N; i++) {
		int a; cin >> a; a--;
		B.push_back(a-i);
	}
	B.push_back(L);
 
	// A[i] and B[i] store the number of holes to the left of position i.
	// (except for end position, there this does not seem correct).

	N += 2;
 
	map<int, int> minA;
	map<int, int> maxA;
	map<int, int> minB;
	map<int, int> maxB;
 
	for (int i = 0; i < N; i++) {
		maxA[A[i]] = i;
		maxB[B[i]] = i;
	}
	for (int i = N-1; i >= 0; i--) {
		minA[A[i]] = i;
		minB[B[i]] = i;
	}

	// minA[h] stores the minimum index at which we
	// have h holes to the left.
	// maxA[h] stores the maximum index at which we have
	// h holes to the left.
	// So if we have 5 penguins in a row without gaps
	// we will have minA[h] = x, maxA[h] = x + 4
 
	// print(A, "A");
	// print(B, "B");
	// print(minA, "minA");
	// print(maxA, "maxA");
	// print(minB, "minB");
	// print(maxB, "maxB");	

	int64_t ans = 0;
	// Loop over minimum index at which we have h holes
	// in the goal state in non-decreasing order of h.
	for (auto it : minB) {
		int v = it.first;
		// cout << "value: " << v << endl;
		if (minA.count(v)) {
			// cout << "minA[v], maxA[v]: " << minA[v] << " " << maxA[v] << endl;
			// cout << "minB[v], maxB[v]: " << minB[v] << " " << maxB[v] << endl;
			// cout << "set minB[" << v << "] to " << min(minB[v], minA[v]) << endl;
			// cout << "set maxB[" << v << "] to " << max(maxB[v], maxA[v]) << endl;
			const int diffA = maxA[v] - minA[v];
			const int diffTotal = max(maxB[v], maxA[v]) - min(minA[v], minB[v]);
			ans += diffTotal - diffA;
			// minB[v] = min(minB[v], minA[v]);
			// maxB[v] = max(maxB[v], maxA[v]);
			// cout << "answer += " << (maxB[v] - minB[v]) - (maxA[v] - minA[v]) << endl;
			// ans += (maxB[v] - minB[v]) - (maxA[v] - minA[v]);
		} else {
			cout << -1 << '\n';
			exit(0);
		}
	}
 
	cout << ans << '\n';
 
	return 0;
}