#include <bits/stdc++.h>

int main(){
    int n, l;
    std::cin >> n >> l;
    std::vector<std::pair<int, int>> vec(n + 2);
    vec[0] = std::make_pair(0, 0);
    vec[n + 1] = std::make_pair(l - n, l - n);
    for(int i = 1; i <= n; i++) std::cin >> vec[i].first, vec[i].first -= i;
    for(int i = 1; i <= n; i++) std::cin >> vec[i].second, vec[i].second -= i;
    long long res = 0;

    for(int i = 1; i <= n;){
        if(vec[i].first < vec[i].second){
            int iter = std::lower_bound(vec.begin() + i, vec.end(), std::make_pair(vec[i].second, -1)) - vec.begin();

            if(vec[i].second == vec[iter].first){
                res += (long long)(iter - i);
                while(i < iter && vec[i].second == vec[iter].first){
                    vec[i].first = vec[i].second;
                    i++;
                }
            }
            else{
                std::cout << "-1\n";
                return 0;
            }
        }
        else i++;
    }

    for(int i = n; i >= 1;){
        if(vec[i].first > vec[i].second){
            int iter = std::upper_bound(vec.begin() + 1, vec.begin() + i + 1, std::make_pair(vec[i].second, 0x7f7f7f7f)) - vec.begin();
            iter--;

            if(vec[i].second == vec[iter].first){
                res += (long long)(i - iter);
                while(i > iter && vec[i].second == vec[iter].first){
                    vec[i].first = vec[i].second;
                    i--;
                }
            }
            else{
                std::cout << "-1\n";
                return 0;
            }
        }
        else i--;
    }

    std::cout << res << "\n";
}

#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using pii = pair<int,int>;

void debug(map<int,pii> a) {
  for (auto p : a) {
    cout << p.first << ": " << "(" << p.second.first << "," << p.second.second << ")\n";
  }
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int N,L;
  cin >> N >> L;

  auto read = [](int N, int L) {
    vector<int> v{0};
    for (int i = 1; i <= N+1; i++) {
      int ai = L+1;
      if (i <= N)
        cin >> ai;
      v.push_back(ai-i);
    }
    map<int,pii> interval;
    for (int i = 0; i < v.size(); i++) {
      if (i==0 || v[i] != v[i-1])
        interval[v[i]].first = i;
      interval[v[i]].second = i;
    }
    return interval;
  };
  auto A = read(N,L);
  auto B = read(N,L);
  ll ans = 0;
  for (auto p : B) {
    int val = p.first;
    if (!A.count(val)) {
      cout << -1 << '\n';
      return 0;
    }
    ans += max(A[val].first-B[val].first,0) + max(B[val].second-A[val].second,0);
  }
  cout << ans << '\n';
}

