#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<db> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 1000000007;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };

int a[100000],b[100000],L[100005],R[100005];
int n,l;
map<int,int> M;
int main(){
	scanf("%d%d",&n,&l);
	fup(i,0,n-1,1)scanf("%d",a+i);
	fup(i,0,n-1,1)scanf("%d",b+i);
	ll ans=0;
	M[1]=-1;
	fup(i,0,n-1,1){
		if(b[i]<a[i]){
			auto it=M.find(b[i]-i);
			if(it==M.end())return !printf("-1\n");
			L[i]=i-it->Y;
		}
		M[a[i]-i]=i;
	}
	int tmp=0;
	fdn(i,n-1,0,1){
		if(L[i]>tmp){
			ans+=L[i];
			tmp=max(0,L[i]-1);
		}else{
			tmp=max(0,tmp-1);
		}
	}
	M.clear();
	M[l+1-n]=n;
	fdn(i,n-1,0,1){
		if(b[i]>a[i]){
			auto it=M.find(b[i]-i);
			if(it==M.end())return !printf("-1\n");
			R[i]=it->Y-i;
		}
		M[a[i]-i]=i;
	}
	tmp=0;
	fup(i,0,n-1,1){
		if(R[i]>tmp){
			ans+=R[i];
			tmp=max(0,R[i]-1);
		}else{
			tmp=max(0,tmp-1);
		}
	}
	printf("%lld\n",ans);
}
/*
after dusk passed,
there is a starry sky.
*/
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define m_k make_pair
#define int long long
using namespace std;
const int N=1e5+100;
int n,l,a[N],b[N],ans;
map <int,vector<int> > mp;
inline int read()
{
	int f=1,x=0;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	return x*f;
}
inline void getup(int l,int r)
{
	for (int i=l;i<=r;)
	{
		int j=i,num=b[i]-i,pos=lower_bound(mp[num].begin(),mp[num].end(),i)-mp[num].begin();
		while(j<=r&&b[j]-j==b[i]-i) j++;
		ans+=mp[num][pos]-i;
		i=j;
	}
}
inline void getdown(int l,int r)
{
	for (int i=r;i>=l;)
	{
		int j=i,num=b[i]-i,pos=upper_bound(mp[num].begin(),mp[num].end(),i)-mp[num].begin()-1;
		while(j>=l&&b[j]-j==b[i]-i) j--;
		ans+=i-mp[b[i]-i][pos];
		i=j;
	}
}
signed main()
{
//	freopen("1.txt","r",stdin);
	n=read();l=read();
	for (int i=1;i<=n;i++) a[i]=read();
	for (int i=1;i<=n;i++) b[i]=read();
	a[0]=0;a[n+1]=l+1;
	for (int i=0;i<=n+1;i++) mp[a[i]-i].push_back(i);
	bool bl=1;
	for (int i=1;i<=n;i++) if (mp[b[i]-i].empty()) bl=0;
	if (!bl)
	{
		printf("-1\n");
		return 0;
	}
	// for (int i=0;i<(int)mp[0].size();i++) printf("%lld ",mp[0][i]);
	// printf("\n");
	for (int i=1;i<=n;)
	{
		int j=i;
		while(j<=n&&(b[j]<=a[j])==(b[i]<=a[i])) j++;
		if (b[i]<=a[i]) getdown(i,j-1);
		else getup(i,j-1);
		i=j;
	}
	printf("%lld\n",ans);
}