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

void fail(){
	cout << -1 << '\n';
	exit(0);
}

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int n, l;
	cin >> n >> l;

	long long ans = 0;
	vector<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[0] = b[0] = 0;
	a[n+1] = b[n+1] = l+1;
	for(int i = 1; i <= n+1; i++){
		a[i] -= i; b[i] -= i;
	}
	l -= n;
	for(int i = 1; i + 1 <= n; i++){
		if(a[i] < b[i] && b[i+1] < a[i+1]){
			fail();
		}
	}
	for(int j = 0; j < 2; j++){
		int cur = n-1;
		for(int i = n; i >= 1; i--){
			if(a[i] > b[i]){
				if(b[i] == b[i-1]){
					ans++;
				} else {
					while(cur >= 0 && a[cur] > b[i]){
						cur--;
					}
					if(cur < 0 || a[cur] != b[i]) fail();
					ans += i - cur;
				}
			}
		}
		reverse(a.begin(), a.end());
		reverse(b.begin(), b.end());
		for(int& x : a) x = l - x;
		for(int& x : b) x = l - x;
	}
	cout << ans << '\n';
}
//雪花飄飄北風嘯嘯
//天地一片蒼茫

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << " is " << x << endl

#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()

#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int n,l;
int arr[100005];
int brr[100005];

map<int,int> mx;
map<int,int> mn;

void rage(){
	cout<<"-1"<<endl;
	exit(0);
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	
	cin>>n>>l;
	rep(x,0,n) cin>>arr[x];
	rep(x,0,n) cin>>brr[x];
	
	rep(x,0,n) if (!mx.count(arr[x]-x)) mx[arr[x]-x]=x;
	if (!mx.count(l-n+1)) mx[l-n+1]=n;
	
	rep(x,n,0) if (!mn.count(arr[x]-x)) mn[arr[x]-x]=x;
	if (!mn.count(1)) mn[1]=-1;
	
	//for (auto &it:mn) cout<<it.fi<<"_"<<it.se<<" "; cout<<endl;
	//for (auto &it:mx) cout<<it.fi<<"_"<<it.se<<" "; cout<<endl;
	
	ll ans=0;
	
	rep(x,0,n){
		if (arr[x]==brr[x]) continue;
		if (arr[x]>brr[x]){ //move stuff left
			if (x!=0 && brr[x-1]==brr[x]-1) ans++;
			else{
				//cout<<"mn: "<<brr[x]-x<<" "<<x<<endl;
				if (!mn.count(brr[x]-x)) rage();
				ans+=x-mn[brr[x]-x];
			}
		}
		else{
			if (x!=n-1 && brr[x]+1==brr[x+1]) ans++;
			else{
				//cout<<"mx: "<<brr[x]-x<<" "<<x<<endl;
				if (!mx.count(brr[x]-x)) rage();
				ans+=mx[brr[x]-x]-x;
			}
		}
	}
	
	cout<<ans<<endl;
}
