#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#define getchar_unlocked getchar

int A[100005];
int B[100005];
int offset[100005];
int minLevel1[100005];
int minLevel2[100005];
int N, L;

inline int nextInt(){
    int x=0;
    char ch=0;
    do{
        ch=getchar_unlocked();
    }while(ch<'0'||ch>'9');
    while(ch>='0'&&ch<='9'){
        x=10*x+(ch-'0');
        ch=getchar_unlocked();
    }
    return x;
}

int main(){
    N = nextInt();
    L = nextInt();
    for(int i = 1; i <= N; i ++){
        A[i] = nextInt();
        offset[i] = A[i]-i;
    }
    for(int i = 1; i <= N; i ++){
        B[i] = nextInt();
    }

    for(int i = 1; i <= N; i ++){
        minLevel1[i] = 0;
        minLevel2[i] = 0;
        //printf("offset[%d]=%d\n", i, offset[i]);
    }

    long long ans = 0;
    int lo = 1;
    int hi2 = 1;
    for(int i = 1; i <= N; i ++){
        if(A[i] == B[i]){continue;}
        if(B[i] == i || B[i] == L-N+i){
            ans ++;
        }else{
            while(lo < N+2 && offset[lo] < B[i]-i){
                lo ++;
            }

            if((lo == N+2) || (offset[lo] != B[i]-i)){
                printf("-1");
                return 0;
            }

            hi2 = max(hi2, lo);
            while(hi2 < N+2 && offset[hi2] <= B[i]-i){
                hi2 ++;
            }

            int hi = hi2-1;
            //int hi = upper_bound(offset+1, offset+(1+N), B[i]-i) - offset - 1;

            if(lo > i){
                minLevel2[lo] = max(minLevel2[lo], lo - i);
            }else{
                minLevel1[hi] = max(minLevel1[hi], i - hi);
            }

            //printf("i=%d [%d, %d]\n", i, lo, hi);
            //ans += min(abs(lo - i), abs(hi - i));
        }
    }

    for(int i = 1; i <= N; i ++){
        ans += minLevel1[i] + minLevel2[i];
    }

    printf("%lld\n", ans);
    return 0;
}

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

const int N=1e5+5;
int n,m,tp[N],a[N],b[N];
long long ans;

void GG(){
	write(-1);
	exit(0);
}

signed main(){
	read(n,m);
	for(int i=1;i<=n;i++) read(tp[i]),a[i]=tp[i]-tp[i-1]-1;a[n+1]=m-tp[n];
	for(int i=1;i<=n;i++) read(tp[i]),b[i]=tp[i]-tp[i-1]-1;b[n+1]=m-tp[n];n++;
	for(int i=1,j=1;i<=n;i++) if(b[i]){
		while(!a[j]) j++;
		int cur=0,l=j;
		while(j<=n&&cur<b[i]) cur+=a[j++];
		if(cur>b[i]) GG();
		ans+=max(i-l,0)+max(j-1-i,0);
	}
	write(ans);
}