#include <algorithm>
#include <cassert>
#include <vector>
#include <iostream>
using namespace std;

long long solve(vector<int>& a, vector<int>& b) {
    // cerr << "a = "; for (int x : a) cerr << ' ' << x; cerr << endl;
    // cerr << "b = ";for (int x : b) cerr << ' ' << x; cerr << endl;
    int k = 0;
    const int n = a.size();
    assert(a[0] != b[0]);
    long long ans = 0;
    for (int i = 0; i < n; ) {
        int prevk = k;
        while (k < n && b[i] + k - i != a[k]) k++;
        if (k == n) {
            if (b[i] + n - 1 == b[n-1] + i) {
                ans += n - i;
                break;
            } else {
                cout << "-1\n"; exit(0);
            }
        }
        if (k > prevk) ans += k - i;
        // cerr << i << " " << k << " " << ans << endl;
        i++;
    }
    // cerr << " => " << ans << endl;
    return ans;
}

int main() {
    int n, L; cin >> n >> L;
    int a[n], b[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < n; i++) cin >> b[i];
    long long ans = 0;
    for (int i = 0;;) {
        while (i < n && a[i] == b[i]) i++;
        if (i == n) break;
        int j = i;
        while (j < n && a[j] != b[j] && ((a[i] < b[i]) == (a[j] < b[j]))) j++;
        vector<int> ta(j-i), tb(j-i);
        for (int k = i; k < j; k++) {
            ta[k-i] = a[k];
            tb[k-i] = b[k];
        }
        if (a[i] > b[i]) {
            if ((i == 0 && b[i] != 1) || (i > 0 && b[i] != b[i-1] + 1)) {
                ans = -1; break;
            }
        } else {
            if ((j == n && b[j-1] != L) || (j < n && b[j-1] != b[j] - 1)) {
                ans = -1; break;
            }
        }

        if (a[i] > b[i]) {
            for (int &x : ta) x = -x;
            for (int &x : tb) x = -x;
            reverse(ta.begin(), ta.end());
            reverse(tb.begin(), tb.end());
        }
        ans += solve(ta, tb);
        i = j;
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ALL(x) begin(x),end(x)
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;
 
template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==(int)v.size()?"":" ");
    return os;
}
template<typename T>
istream &operator>>(istream &is,vector<T>&v){
    for(T &x:v)is>>x;
    return is;
}


signed main(){
    int n;ll l;cin>>n>>l;
    vector<ll> a(n),b(n);
    cin>>a>>b;

    ll p=0;
    vector<ll> A,B;
    rep(i,n){
        A.push_back(a[i]-p-1);
        p=a[i];
    }
    A.push_back(l-a.back());
    p=0;
    rep(i,n){
        B.push_back(b[i]-p-1);
        p=b[i];
    }
    B.push_back(l-b.back());
    ll res=0;
    for(int i=0,ai=0;i<(int)B.size();i++)if(B[i]){
        while(A[ai]==0) ai++;
        ll sum=0;
        ll l=ai;
        while(sum<B[i]) sum+=A[ai++];
        res+=max(0ll,i-l);
        res+=max(0,ai-i-1);
        if(sum>B[i]){
            cout<<-1<<endl;
            return 0;
        }
    }
    cout<<res<<endl;
    return 0;
}
