#include <bits/stdc++.h>

using namespace std;

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")


using ll = long long;
using ld = long double;



#define mp make_pair

const int p = 998244353;

int mul(int a, int b) {
    return (1LL * a * b) % p;
}

int add(int a, int b) {
    int s = (a+b);
    if (s>=p) s-=p;
    return s;
}

int sub(int a, int b) {
    int s = (a+p-b);
    if (s>=p) s-=p;
    return s;
}

int po(int a, int deg)
{
    if (deg==0) return 1;
    if (deg%2==1) return mul(a, po(a, deg-1));
    int t = po(a, deg/2);
    return mul(t, t);
}

int inv(int n)
{
    return po(n, p-2);
}


mt19937 rnd(time(0));

/*
const int N = 1200000;

vector<int> facs(N), invfacs(N);

void init()
{
    facs[0] = 1;
    for (int i = 1; i<N; i++) facs[i] = mul(facs[i-1], i);
    invfacs[N-1] = inv(facs[N-1]);
    for (int i = N-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}

int C(int n, int k)
{
    if (n<k) return 0;
    if (n<0 || k<0) return 0;
    return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}

*/


/*struct DSU
{
    vector<int> sz;
    vector<int> parent;

    void make_set(int v) {
        parent[v] = v;
        sz[v] = 1;
    }

    int find_set(int v) {
        if (v == parent[v])
            return v;
        return parent[v] = find_set(parent[v]);
    }

    void union_sets(int a, int b) {
        a = find_set(a);
        b = find_set(b);
        if (a != b) {
            if (sz[a] < sz[b])
                swap(a, b);
            parent[b] = a;
            sz[a] += sz[b];
        }
    }

    DSU (int n)
    {
        parent.resize(n);
        sz.resize(n);
        for (int i = 0; i<n; i++) make_set(i);
    }
};*/

int solve(int l, int r, int pos)
{
    return max(r, pos) - min(l, pos);
}


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);

    int n, l;
    cin>>n>>l;
    vector<int> a(n+2), b(n+2);
    for (int i = 1; i<=n; i++) cin>>a[i];
    for (int i = 1; i<=n; i++) cin>>b[i];
    a[0] = 0; b[0] = 0; a[n+1] = l+1; b[n+1] = l+1;
    vector<int> A;
    for (int i = 0; i<n+1; i++) A.push_back(a[i+1]-a[i]-1);
    vector<int> B;
    for (int i = 0; i<n+1; i++) B.push_back(b[i+1]-b[i]-1);

    a = A; b = B;

    ll ans = 0;

    n++;
    int cur = 0;
    for (int i = 0; i<n; i++) if (b[i]!=0)
    {
        while (cur<n && a[cur]==0) cur++;

        int l = cur;
        while (b[i]>0)
        {
            if (b[i]<a[cur]) {cout<<-1; return 0;}
            b[i]-=a[cur];
            if (b[i]==0)
            {
                ans+=max(i, cur) - min(i, l);
                cur++;
                break;
            }
            cur++;
        }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;using ll=long long;using uint=unsigned int;using pii=pair<int,int>;using pll=pair<ll,ll>;using ull = unsigned long long;using ld=long double;template<typename T>void _(const char*s,T h){cerr<<s<<" = "<<h<<"\n";}template<typename T,typename...Ts>void _(const char*s,T h,Ts...t){int b=0;while(((b+=*s=='(')-=*s==')')!=0||*s!=',')cerr<<*s++;cerr<<" = "<<h<<",";_(s+1,t...);}// break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i,n) for(int i=0;i<(n);++i)
#define sz(a)((int)(a).size())
#define sqr(x) ((x)*(x))
struct init{init(){cin.tie(0);iostream::sync_with_stdio(0);cout<<fixed<<setprecision(10);cerr<<fixed<<setprecision(5);}~init(){
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__,__VA_ARGS__)
cerr<<"Time elapsed: "<<(double)clock()/CLOCKS_PER_SEC<<"s.\n";
#else
#define dbg(...) 
#endif
}}init;template<typename T,typename U>void upx(T&x,U y){if(x<y)x=y;}template<typename T,typename U>void upn(T&x,U y){if(x>y)x=y;}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());const int D=4,dx[]={+1,0,-1,0},dy[]={0,+1,0,-1};

const int N=1e5+5;
int a[N],b[N],p[N];
void no(){
	cout<<"-1\n";
	exit(0);
}
int f[N];
int g(int l,int r){
	if(l>r)return 0;
	return f[r]-(l==0?0:f[l-1]);
}

int32_t main(){
	int n,l;
	cin>>n>>l;
	for(int i=1;i<=n;++i)cin>>a[i];
	for(int i=1;i<=n;++i)cin>>b[i];
	a[n+1]=b[n+1]=l+1;
	map<int,int> mp;
	mp[0]=0;
	for(int i=1;i<=n;++i){
		p[i]=-1;
		mp[a[i]-i]=i;
		if(mp.find(b[i]-i)!=mp.end()){
			p[i]=mp[b[i]-i];
		}
	}
	mp.clear();
	mp[a[n+1]-(n+1)]=n+1;
	for(int i=n;i>=1;--i){
		mp[a[i]-i]=i;
		if(p[i]==-1&&mp.find(b[i]-i)!=mp.end()){
			p[i]=mp[b[i]-i];
		}
		if(p[i]==-1)no();
	}
	p[0]=0;
	p[n+1]=n+1;
	for(int i=1;i<=n;++i){
		if(p[i]>p[i+1])no();
		f[p[i]]=1;
	}
	for(int i=1;i<=n+1;++i)f[i]+=f[i-1];
	int ans=0;
	for(int i=1;i<=n;++i){
		if(i==p[i])continue;
		if(i<p[i])ans+=g(i+1,p[i]);
		if(i>p[i])ans+=g(p[i],i-1);
	}
	cout<<ans<<'\n';

    return 0;
}
