#include<bits/stdc++.h>

int main() {
	using namespace std;
	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);

	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;
	}

	int64_t ans = 0;
	for (auto it : minB) {
		int v = it.first;
		if (minA.count(v)) {
			minB[v] = min(minB[v], minA[v]);
			maxB[v] = max(maxB[v], maxA[v]);
			ans += (maxB[v] - minB[v]) - (maxA[v] - minA[v]);
		} else {
			cout << -1 << '\n';
			exit(0);
		}
	}

	cout << ans << '\n';

	return 0;
}

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<tuple>
#include<cassert>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define Per(i,sta,n) for(int i=n-1;i>=sta;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
int dx[4]={1,-1,0,0};
int dy[4]={0,0,1,-1};
template<class T>bool chmax(T &a, const T &b) {if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a, const T &b) {if(b<a){a=b;return 1;}return 0;}

int n,l;
int a[100010],b[100010];
int d[100010];
map<int,bool> ext;
map<int,int> L,R;
map<int,vector<int>> V;

void solve(){
    cin >> n >> l;
    rep(i,n) cin >> a[i+1];
    rep(i,n) cin >> b[i];
    a[n+1]=l+1;
    rep(i,n+2){
        if(!ext[a[i]-i]){
            L[a[i]-i]=i;
            R[a[i]-i]=i;
        }
        else{
            chmax(R[a[i]-i],i);
        }
        ext[a[i]-i]=true;
    }
    ll ans=0;
    // for(auto p:ext){
    //     cout << p.first << " " << L[p.first] << " " << R[p.first] << endl;
    // }
    rep(i,n){
        if(!ext[b[i]-i-1]){
            cout << -1 << endl;
            return;
        }
        V[b[i]-i-1].push_back(i+1);
    }
    for(auto p:V){
        int t=p.first;
        // cout << t << endl;
        // for(int i:p.second) cout << i << " ";
        //cout << "" << endl;
        if(L[t]>p.second.back()){
            ans+=L[t]-p.second[0];
        }
        else if(R[t]<p.second[0]){
            ans+=p.second.back()-R[t];
        }
        else{
            ans+=max(0,L[t]-p.second[0])+max(0,p.second.back()-R[t]);
        }
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(50);
    solve();
}