#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
#define INF 1000000001

int main() {
    int N,M; cin>>N>>M;
    vi H(N), W(M);
    for (int i = 0; i < N; i++)cin>>H[i];
    for (int i = 0; i < M; i++)cin>>W[i];

    sort(H.begin(),H.end());
    sort(W.begin(),W.end());
    vi pair_sum((N-1)/2+1, 0);
    for (int i = 1; i <= (N-1)/2; i++){
        pair_sum[i]=pair_sum[i-1]+H[N-1-2*(i-1)]-H[N-2-2*(i-1)];
    }
    priority_queue<int, vi, greater<int>> p;
    for (int i = 1; i < N; i++)p.push(H[i]);
    int waited_partner=H[0];
    int made_pair=0;
    int made_pair_num=0;
    int next=INF;
    int ans=INF;
    for (int i = 0; i < M; i++){
        int join=W[i];
        if(p.empty()){
            ans=min(ans, made_pair+abs(join-waited_partner));
            continue;
        }
        next=p.top();
        while(join>next){
            made_pair+=next-waited_partner; p.pop();
            made_pair_num++;
            waited_partner=p.top(); p.pop();
            if(!p.empty())next=p.top();
            else next=INF;
        }
        ans=min(ans, made_pair+abs(join-waited_partner)+pair_sum[(N+1)/2-1-made_pair_num]);
    }

    cout<<ans<<endl;
}
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define ll long long
#define pp pair<ll,ll>
#define ld long double
#define all(a) (a).begin(),(a).end()
#define mk make_pair
int inf=1000001000;
ll INF=2e18;
ll MOD=1000000007;
ll mod=998244353;

int main() {
    int n,l;
    cin >> n >> l;
    vector<int> a(n+2),b(n+2);
    rep(i,n) cin >> a[i+1];
    rep(i,n) cin >> b[i+1];
    a[0]=0;
    a[n+1]=l+1;
    b[0]=0;
    b[n+1]=l+1;
    ll ans=0;
    for (int i=1;i<=n;i++){
        int l=-1,r=n+1;
        while(r-l>1){
            int mid=(r+l)/2;
            if (mid<=i) {
                l=mid;
                continue;
            }
            if (a[mid]-(mid-i)>=b[i]) r=mid;
            else l=mid;
        }
        if (a[r]-(r-i)==b[i] && a[i]!=b[i]){
            rep(j,r-i){
                a[r-j-1]=a[r]-j-1;
                ans++;
            }
        }
    }
    for (int i=n;i>=1;i--){
        int l=0,r=n+2;
        while(r-l>1){
            int mid=(r+l)/2;
            if (mid>=i) {
                r=mid;
                continue;
            }
            if (a[mid]+(i-mid)<=b[i]) l=mid;
            else r=mid;
        }
        if (a[l]+(i-l)==b[i] && a[i]!=b[i]){
            rep(j,i-l){
                a[l+j+1]=a[l]+j+1;
                ans++;
            }
        }
    }
    bool w=true;
    rep(i,n){
        if (a[i+1]!=b[i+1]) w=false;
    }
    if (w) cout << ans << endl;
    else cout << -1 << endl;
}
