#include <bits/stdc++.h>

using namespace std;

#define int long long

int N,L;
vector<int> A(1e5+10), B(1e5+10), C, high(1e5+10), low(1e5+10);

main() {

    cin >> N >> L;
    C.push_back(0);
    for (int i = 1; i <= N; i++) {
        cin >> A[i];
        C.push_back(A[i]-i);
    }
    C.push_back(L-N);
    
    int t = 0;
    for (int i = 1; i <= N; i++) {
        cin >> B[i];
      //  cout << "Goal: " << " " << B[i] << "\n";
      //  for (int c: C) cout << c+i << " "; cout << "\n";
        auto it = lower_bound(C.begin(), C.end(), B[i]-i);
        if (it == C.end() || *it != B[i]-i) {
            cout << "-1\n";
            return 0;
        }
        auto it2 = upper_bound(C.begin(), C.end(), B[i]-i);
        int z = it-C.begin();
        int z2 = it2-C.begin()-1;
        if (z <= i && i <= z2) {
            z = i;
        }
        else if (abs(z2-i) < abs(z-i)) {
            z = z2;
        }
        //go from i to z
        //cout << i << " to " << z << "\n";
        if (i < z) {
            low[z] = max(z-i, low[z]);
        }
        if (i > z) {
            high[z] = max(i-z,high[z]);
        }
    }

    for (int i = 0; i <= N+1; i++) {
       // cout << i << " " << low[i] << " " << high[i] << "\n";
        t += low[i] + high[i];
    }

    cout << t << "\n";

   // cout << t << "\n";



    return 0;
}
#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
 
using namespace std;
using ll = long long;
using ld = long double;
ll MOD = 998244353;
const ll root = 3;
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 100001;
int dir[mxN];
int a[mxN], b[mxN];
int n, L;
void killthyself() {
    cout<<"-1\n";
    exit(0);
}
ll getright(int l, int r) {
    ll res = 0;
    int right = l;
    int prv = l;
    owo(left, l, r) {
        while(right <= r && a[right] - b[left] != right - left)right++;
        if(a[right] - b[left] != right - left)killthyself();
        //move everyone from l-r forward
        if(right!=prv)res += right - left;
        //cout<<right<<" "<<prv<<" Hi\n";
        prv = right;
    }
    //cout<<"right "<<l<<" "<<r<<" "<<res<<"\n";
    return res;
}
ll getleft(int l, int r) {
    ll res = 0;
    int left = r;
    int prv = r;
    uwu(right, r+1, l) {
        while(left >= l && b[right] - a[left] != right - left)left--;
        if(b[right] - a[left] != right - left)killthyself();
        if(left!=prv)res += right - left;
        prv = left;
    }
    //cout<<"left "<<l<<" "<<r<<" "<<res<<"\n";
    return res;
}
ll go(int l, int r) {
    if(r-l < 2)return 0;
    int suff = r-1;
    int pref = l+1;
    while(dir[suff]==-1) {
        suff--;
    }
    while(dir[pref]==1) {
        pref++;
    }
    if(pref-suff!=1)killthyself();
    return getright(suff+1, r) + getleft(l, pref-1);
}
void solve() {
    cin>>n>>L;
    a[0] = 0;
    a[n+1] = L+1;
    owo(i, 1, n+1)cin>>a[i];
    owo(i, 1, n+1) {
        cin>>b[i];
        if(a[i]==b[i])dir[i] = 0;
        else dir[i] = (a[i] - b[i] > 0) - (b[i] - a[i] > 0);
    }
    ll res = 0;
    int prv = 0;
    owo(i, 1, n+2) {
        if(dir[i]==0) {
            res += go(prv, i);
            prv = i;
        }
    }
    cout<<res<<"\n";
}
int main() {
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    int T = 1;
    owo(tc, 1, T+1) {
        solve();
    }
    return 0;
}