#include<cstdio>
#include<algorithm>
#include<cctype>
#include<set>
using namespace std;
#define G getchar()
int read()
{
	int x=0; char ch=G;
	for (;!isdigit(ch);ch=G);
	for (;isdigit(ch);ch=G) x=(x<<3)+(x<<1)+(ch^48);
	return x;
}
#undef G
typedef long long ll;

int n,L;  //[0,L]
int w[100010],t[100010];
set<int> st;
int que[100010],hd,tl;
ll ans;
int dir[100010];
int main()
{
	n=read(),L=read()-n;
	for (int i=1;i<=n;i++) w[i]=read()-i;
	set<int>(w+1,w+n+1).swap(st);
	st.insert(0); st.insert(L);
	for (int i=1;i<=n;i++){
		int x=t[i]=read()-i;
		if (!st.count(x)) return puts("-1"),0;
	}
//	for (int i=1;i<=n;i++) printf("%d: %d %d\n",i,w[i],t[i]);
	for (int i=1;i<=n;i++)
		if (t[i]<w[i]) dir[i]=-1;
		else if (t[i]>w[i]) dir[i]=1;
	for (int i=1,j;i<=n;i=j+1){
		for (j=i;j<n&&dir[j+1]==dir[j];j++);
//		printf("solve %d %d: %d\n",i,j,dir[i]);
		if (dir[i]==0) continue;
		if (dir[i]==1){
			hd=tl=0;
			for (int u=i,v;u<=j;u=v+1){
				for (v=u;v<j&&w[v+1]==w[v];v++);
				for (int e=u;e<=v;e++) que[++tl]=e;
				bool flg=0;
				while (hd<tl&&t[que[hd+1]]==w[u]) flg=1,hd++;
				if (flg) ans+=tl-hd;
				else ans+=v-u+1;
			}
		}
		else{
			hd=tl=0;
			for (int u=j,v;u>=i;u=v-1){
				for (v=u;v>i&&w[v-1]==w[v];v--);
				for (int e=u;e>=v;e--) que[++tl]=e;
				bool flg=0;
				while (hd<tl&&t[que[hd+1]]==w[u]) flg=1,hd++;
				if (flg) ans+=tl-hd;
				else ans+=u-v+1;
			}
		}
	}
	printf("%lld\n",ans);
	return 0;
}
#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#define ll long long int
#define ld long double
#define vll vector<ll>
#define vvll vector<vector<ll>>
#define vpll vector<pair<ll,ll>>
#define prqueue priority_queue<ll,vector<ll>,greater<ll>>// top=small
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define f0(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<=n;i++)
#define fab(i,a,b) for(i=a;i<=b;i++)
#define f0r(i,n) for(i=n-1;i>=0;i--)
#define f1r(i,n) for(i=n;i>=1;i--)
#define fabr(i,a,b) for(i=b;i>=a;i--)
#define memo(a,b) memset(a,b,sizeof(a))
#define display(x) {for(auto dsp:x)cout<<dsp<<" ";cout<<"\n";}
#define hi cout<<"hi\n"
#define yes cout<<"Yes\n"
#define no cout<<"No\n"
#define ln "\n"
using namespace std;
const ld pi=acos(-1);const ll mod=1e9+7;bool debug=false;

int main()
{FAST;ll t=1,n,i,j,k,len,x,y,z,c,f,flag,p,q,mx,mn,l,r,sum,ans,tmp,it,pos,avg,m,cnt;
    string s;char ch;vll v;vpll vec;unordered_map<ll,ll> mappu;pair<ll,ll> pr;
    // cin>>t;while(t--)
    {
    	n=3;//3
        f=0;sum=0;flag=0;ans=0;cnt=0;v.clear();mappu.clear();vec.clear();
        ll a[n];
        f0(i,n)
        {
            cin>>tmp;
            a[i]=tmp;
            // mappu[tmp]++;
        }
        
        sort(a,a+n);
        
        if(a[2]-a[1]==a[1]-a[0]) yes;
        else no;
        
        
        
        
        
        
        // cout<<ans<<ln;
    }
	return 0;
}
// Either be on top or leave this crap..