#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

ll n, l, a[100008], b[100008];

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    
    cin >> n >> l;
    rep(i, 1, n)cin >> a[i];
    rep(i, 1, n)cin >> b[i];
    a[0] = 0;
    b[0] = 0;
    a[n + 1] = l + 1;
    b[n + 1] = l + 1;
    n += 2;

    vector<ll> f;
    zep(i, 0, n)if(a[i] == b[i])f.push_back(i);

    ll m = f.size();
    
    ll ans = 0;
    zep(i, 0, m - 1){
        ll B = f[i];
        bool flag = false;
        rep(j, f[i] + 1, f[i + 1] - 1){
            if(b[j] < a[j]){
                if(flag){print(-1) return 0;}
                B = j;
            }else{
                flag = true;
            }
        }

        map<ll, ll> L;
        L[b[f[i]] - f[i]] = f[i];
        rep(j, f[i] + 1, B){
            if(L.find(b[j] - j) == L.end()){
                print(-1) return 0;
            }else{
                ans += j - L[b[j] - j];
                L[b[j] - j] = j;
            }

            L[a[j] - j] = j;
        }
        //print(ans)

        map<ll, ll> R;
        R[b[f[i + 1]] - f[i + 1]] = f[i + 1];
        rrep(j, f[i + 1] - 1, B + 1){
            if(R.find(b[j] - j) == R.end()){
                print(-1) return 0;
            }else{
                ans += R[b[j] - j] - j;
                R[b[j] - j] = j;
            }

            R[a[j] - j] = j;
        }
        //print(ans)
    }
    
    print(ans)

    return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll> pql;
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++)
#define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--)
#define all(v) (v).begin(), (v).end()
ll mod(ll a, ll b) {if(b == 0) return 0; return (a % b + b) % b;}
bool chmin(ll &a, ll b) {if(b < a) {a = b; return 1;} return 0;}
bool chmax(ll &a, ll b) {if(b > a) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 1e5;
const char newl = '\n';

ll n, l;
bool t=1;
vl a(MAX+2), b(MAX+2), d(MAX+2);

ll go_right(ll L, ll r) {
  ll cnt = 0;
  rep2(l, L, r-1) {
    if(a[l] == b[l]) continue;
    ll from=l, to=r;
    while(to-from>1) {
      ll m = (from + to)/2;
      if(b[l] <= a[m] - m + l) to = m;
      else from = m;
    }
    if(b[l] != a[to] - to + l) {t=0; return 0;}
    rep2(i, l, to-1) a[i] = a[to] - to + i;
    cnt += to - l;
  }
  return cnt;
}

ll go_left(ll l, ll R) {
  ll cnt = 0;
  rep3(r, R, l+1) {
    if(a[r] == b[r]) continue;
    ll from=l, to=r;
    while(to-from>1) {
      ll m = (from + to)/2;
      if(b[r] >= a[m] - m + r) from = m;
      else to = m;
    }
    if(b[r] != a[from] - from + r) {t=0; return 0;}
    rep3(i, r, from+1) a[i] = a[from] - from + i;
    cnt += r - from;
  }
  return cnt;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> l;
  a[0] = 0;
  a[n+1] = l+1;
  b[0] = 0;
  b[n+1] = l+1;
  rep2(i, 1, n) cin >> a[i];
  rep2(i, 1, n) cin >> b[i];
  rep(i, n+2) d[i] = b[i] - a[i];

  rep2(i, 1, n-1) if(d[i] > 0 && d[i+1] < 0) {cout << -1 << newl; return 0;} 

  ll ans=0;
  for(ll r=0; r<=n+1;) {
    if(d[r]==0) r++;

    ll l=r;
    if(d[r]>0) {
      while(d[r]>0) r++;
      ans += go_right(l, r);
    }
    if(d[r]<0) {
      while(d[r]<0) r++;
      ans += go_left(l-1, r-1);
    }

    if(!t) {
      cout << -1 << newl;
      return 0;
    }
  }
  cout << ans << newl;
  return 0;
}