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

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end



int main(){
   int n,L;
   cin>>n>>L;
   vector<int> a(n+2),b(n+2);
   rep(i,1,n+1)cin>>a[i];
   rep(i,1,n+1)cin>>b[i];
   a[n+1]=a[n+1]=L+1;

   map<int,vector<int>> pos;
   rep(i,0,n+2)pos[a[i]-i].push_back(i);

   ll res=0;
   rep(i,1,n+1){
      if(a[i]==b[i])continue;
      int x=b[i]-i;
      if(x<0 or pos[x].empty()){
         puts("-1");
         return 0;
      }
      if(a[i]<b[i]){
         if(b[i]==b[i+1]-1){
            res++;
            continue;
         }
         int j=pos[x].front();
         res+=j-i;
      }
      else{
         if(b[i]==b[i-1]+1){
            res++;
            continue;
         }
         int j=pos[x].back();
         res+=i-j;
      }
   }
   cout<<res<<'\n';
   return 0;
}
#include<bits/stdc++.h>

using namespace std;
using ll = long long int;
using lc = complex<double>;

template<class T>bool chmin(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }

int main(void) {
    constexpr ll MOD = 1e9 + 7;
    constexpr double PI = acos(-1);
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);

    ll n, l;
    cin >> n >> l;
    vector<ll> a(n+2), b(n+2);
    for(ll i=0; i<n; i++) cin >> a[i+1];
    for(ll i=0; i<n; i++) cin >> b[i+1];
    a[0] = b[0] = 0;
    a[n+1] = b[n+1] = l+1;

    n+=2;

    ll res = 0;
    /* { */
    /*     ll i = 0; */
    /*     while(i<n && a[i]>=b[i]) i++; */
    /*     while(i<n && a[i]<=b[i]) i++; */
    /*     if(i != n) goto ng; */
    /* } */

    cerr << "1st" << endl;

    {
        for(ll i=0; i<n-1; i++) {
            if(a[i] < b[i]) {
                ll j = upper_bound(a.begin(), a.end(), b[i]) - a.begin();
                while(j<n && !(j-i == a[j]-b[i])) j++;
                if(j == n) goto ng;
                cerr << i << ' ' << j << endl;
                res += j-i;

                while(i<j && b[i+1] == b[i]+1) i++;
            }
        }
    }

    cerr << "2nd" << endl;

    {
        for(ll i=0; i<n; i++)
            a[i] = l+1 - a[i], b[i] = l+1 - b[i];
        reverse(a.begin(), a.end());
        reverse(b.begin(), b.end());

        for(ll i=0; i<n-1; i++) {
            if(a[i] < b[i]) {
                ll j = upper_bound(a.begin(), a.end(), b[i]) - a.begin();
                while(j<n && !(j-i == a[j]-b[i])) j++;
                if(j == n) goto ng;
                cerr << i << ' ' << j << endl;
                res += j-i;

                while(i<j && b[i+1] == b[i]+1) i++;
            }
        }
    }

    cout << res << endl;

    return 0;

ng:
    cout << -1 << endl;
    return 0;
}