#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;
}
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int, int>
#define ull unsigned ll
#define f first
#define s second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define SQ(x) (x)*(x)
#define MN(a,b) a = min(a,(__typeof__(a))(b))
#define MX(a,b) a = max(a,(__typeof__(a))(b))
#define pb push_back
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#ifdef BALBIT
#define IOS()
#define bug(...) fprintf(stderr,"#%d (%s) = ",__LINE__,#__VA_ARGS__),_do(__VA_ARGS__);
template<typename T> void _do(T &&x){cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y){cerr<<x<<", ";_do(y...);}
#else
#define IOS() ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define bug(...)
#endif

const int iinf = 1e9+10;
const ll inf = 1ll<<60;
const ll mod = 1e9+7 ;


void GG(){cout<<"0\n"; exit(0);}

ll mpow(ll a, ll n, ll mo = mod){ // a^n % mod
    ll re=1;
    while (n>0){
        if (n&1) re = re*a %mo;
        a = a*a %mo;
        n>>=1;
    }
    return re;
}

ll inv (ll b, ll mo = mod){
    if (b==1) return b;
    return (mo-mo/b) * inv(mo%b,mo) % mo;
}

const int maxn = 1e6+5;


signed main(){
    IOS();
    int n,l; cin>>n>>l;
    vector<int> a(n);
    map<int,int> rch;
    map<int, int> r2;
    rch[1] = r2[1] = 0;
    rch[(l-n+1)] = r2[l-n+1] = n+1;
    for (int i = 0; i<n; ++i) {
        cin>>a[i]; a[i] -= i;
        bug(a[i]);
        if (!r2.count(a[i])) r2[a[i]] = i+1;
        else
            MX(r2[(a[i])], i+1);
        if (!rch.count(a[i])) rch[a[i]] = i+1;
        else
            MN(rch[(a[i])], i+1);
    }
    vector<int> b(n);
    vector<int> mn(n+2, 10000000), mx(n+2, -10000000);
    for (int i = 0; i<n+2; ++i) mn[i] = mx[i] = i-1;
    for (int j = 0; j<n; ++j) {
        cin>>b[j]; b[j] -= j;
        bug(b[j]);
        if (!rch.count(b[j])) {
            cout<<-1<<endl; return 0;
        }
        int L = rch[b[j]]-1, R = r2[b[j]]-1;
        int k = L;
        if (j >= L && j <= R) k = j;
        else if (j > R) k = R;
        ++k;
        MN(mn[k], j);
        MX(mx[k], j);
    }
    ll re = 0;
    for (int i = 0; i<=n+1; ++i) {

        re += max(0ll,mx[i] - mn[i]);
    }
    cout<<re<<endl;

}
