#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

int N, M, K, T, H, W, L, R;
//long long int N, M, K, T, H, W, L, R;

vector<int>lft;
vector<int>rht;
bool flag;

long long int Left(vector<int>&v, vector<int>&w, int st, int ed) {
	int cnt = st - 1;
	for (int i = st; i <= ed; i++) {
		while (cnt < i&&v[cnt] + i - cnt != w[i]) {
			cnt++;
		}
		while (cnt + 1 < i&&v[cnt + 1] + i - cnt - 1 == w[i]) {
			cnt++;
		}
		if (cnt == i) {
			flag = false;
			break;
		}
		lft[i] = cnt;
	}
	long long int ret = 0;
	if (!flag)return 0;
	int bef = -1;
	for (int i = ed; i >= st; i--) {
		if (lft[i] != bef) {
			ret += i - lft[i];
		}
		bef = lft[i];
	}
	return ret;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N >> M;
	flag = true;
	vector<int>v(N + 2);
	vector<int>w(N + 2);
	lft.resize(N + 2);
	rht.resize(N + 2);
	for (int i = 1; i <= N; i++) {
		cin >> v[i];
	}
	v.back() = M + 1;
	for (int i = 1; i <= N; i++) {
		cin >> w[i];
	}
	w.back() = M + 1;
	auto revv = v;
	auto revw = w;
	for (auto &i : revv) {
		i = M + 1 - i;
	}
	for (auto &i : revw) {
		i = M + 1 - i;
	}
	reverse(revv.begin(), revv.end());
	reverse(revw.begin(), revw.end());
	vector<int>same;
	for (int i = 0; i < N + 2; i++) {
		if (v[i] == w[i])same.push_back(i);
	}
	long long int ans = 0;
	for (int i = 1; i < same.size(); i++) {
		int st = same[i - 1] + 1;
		int ed = same[i] - 1;
		if (v[st] > w[st]) {
			if (v[ed] > w[ed]) {
				ans += Left(v, w, st, ed);
			}
			else {
				int box = st;
				for (int j = st; j <= ed; j++) {
					if (v[j] > w[j])box = j;
					else break;
				}
				ans += Left(v, w, st, box);
				ans += Left(revv, revw, N + 1 - ed, N + 1 - box - 1);
			}
		}
		else {
			if (v[ed] > w[ed]) {
				cout << -1 << endl;
				return 0;
			}
			ans += Left(revv, revw, N + 1 - ed, N + 1 - st);
		}
	}
	if (flag) {
		cout << ans << endl;
	}
	else {
		cout << -1 << endl;
	}
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
const int INF=1000000000+5;
const int N=1e5+5;
const ll oo=1e18+5;
const ll mod=1e9+7;

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

int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	int n,L;
	cin>>n>>L;
	auto a=get(n,L);
	auto b=get(n,L);
	//~ cout<<"a : "<<endl;
	//~ for(auto x:a){
		//~ cout<<"("<<x.first<<")["<<x.second.first<<","<<x.second.second<<"]"<<endl;
	//~ }
	//~ cout<<"b : "<<endl;
	//~ for(auto x:b){
		//~ cout<<"("<<x.first<<")["<<x.second.first<<","<<x.second.second<<"]"<<endl;
	//~ }
	ll ans=0;
	for(auto x:b){
		int val=x.first;
		if(!a.count(val)){
			return cout<<-1,0;
		}
		ans+=max(0,a[val].first	-b[val].first)+max(0,b[val].second-a[val].second);
	}
	cout<<ans;
	return 0;
}
