#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=200005;
int n,l,ans,sum,a[N],b[N],c[N],d[N];
signed main(){
	scanf("%lld%lld",&n,&l);
	for (int i=1;i<=n;i++)scanf("%lld",&a[i]);
	for (int i=1;i<=n;i++)scanf("%lld",&b[i]);
	a[n+1]=b[n+1]=l+1;
	for (int i=0;i<=n;i++)c[i]=a[i+1]-a[i]-1,d[i]=b[i+1]-b[i]-1;
	for (int i=0,j=0;i<=n;i++)
		if (d[i]){
			while (c[j]==0)j++;
			l=j;sum=0;
			while (j<=n&&sum<d[i])sum+=c[j++];
			if (sum!=d[i]){
				puts("-1");
				return 0;
			}
			ans+=max(i-l,0ll)+max(j-i-1,0ll);
		}
	printf("%lld",ans);
} 

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ldb double
template<typename T>void ckmn(T&a,T b){a=min(a,b);}
template<typename T>void ckmx(T&a,T b){a=max(a,b);}
void rd(int&x){scanf("%i",&x);}
void rd(ll&x){scanf("%lld",&x);}
void rd(char*x){scanf("%s",x);}
void rd(ldb&x){scanf("%lf",&x);}
void rd(string&x){scanf("%s",&x);}
template<typename T1,typename T2>void rd(pair<T1,T2>&x){rd(x.first);rd(x.second);}
template<typename T>void rd(vector<T>&x){for(T&i:x)rd(i);}
template<typename T,typename...A>void rd(T&x,A&...args){rd(x);rd(args...);}
template<typename T>void rd(){T x;rd(x);return x;}
int ri(){int x;rd(x);return x;}
template<typename T>vector<T> rv(int n){vector<T> x(n);rd(x);return x;}
template<typename T>void ra(T a[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]);}
template<typename T1,typename T2>void ra(T1 a[],T2 b[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]);}
template<typename T1,typename T2,typename T3>void ra(T1 a[],T2 b[],T3 c[],int n,int st=1){for(int i=0;i<n;++i)rd(a[st+i]),rd(b[st+i]),rd(c[st+i]);}
void re(vector<int> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){rd(u,v);E[u].pb(v);if(!dir)E[v].pb(u);}}
template<typename T>void re(vector<pair<int,T>> E[],int m,bool dir=0){for(int i=0,u,v;i<m;++i){T w;rd(u,v,w);E[u].pb({v,w});if(!dir)E[v].pb({u,w});}}

void NO(){printf("-1\n");exit(0);}
ll Solve(vector<pii> pts){
	if(pts[0].first==pts[0].second){
		for(pii&p:pts)p.first*=-1,p.second*=-1;
		reverse(pts.begin(),pts.end());
	}
	ll ans=0;
	for(int i=0,las=-1;i+1<pts.size();i++){
		int bot=i,top=pts.size()-1,mid,ptr=pts.size();
		while(top>=bot){
			mid=top+bot>>1;
			int o=pts[mid].first-(mid-i);
			if(o>=pts[i].second)ptr=mid,top=mid-1;
			else bot=mid+1;
		}
		if(ptr==pts.size()||pts[ptr].first-(ptr-i)!=pts[i].second)NO();
		if(las!=ptr)ans+=ptr-i;
		las=ptr;
	}
	return ans;
}
vector<int> zero;
const int N=100050;
int a[N],b[N];
int main(){
	int n,l;rd(n,l);
	ra(a,n);
	ra(b,n);
	a[n+1]=b[n+1]=l+1;
	for(int i=0;i<=n+1;i++)if(a[i]==b[i])zero.pb(i);
	ll ans=0;
	for(int i=1;i<zero.size();i++){
		int l=zero[i-1]+1,r=zero[i]-1;
		vector<pii> pts;
		pts.pb({a[l-1],b[l-1]});
		int L=l;while(a[L]>b[L])pts.pb({a[L],b[L]}),L++;
		ans+=Solve(pts);
		pts.clear();
		pts.pb({a[r+1],b[r+1]});
		int R=r;while(a[R]<b[R])pts.pb({a[R],b[R]}),R--;
		reverse(pts.begin(),pts.end());
		ans+=Solve(pts);
		if(L!=R+1)NO();
	}
	printf("%lld\n",ans);
	return 0;
}