#include <bits/stdc++.h>

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

pii irange(const vi64 &a, int x) {
    auto w = equal_range(all(a), x);
    return {w.fi - a.begin(), w.se - a.begin()};
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "rt", stdin);
#endif

    int n, l;
    cin >> n >> l;
    vi64 a = {0}, b = {0};
    forn(i, n) {
        int x;
        cin >> x;
        a.pb(x - i - 1);
    }
    a.pb(l - n);
    forn(i, n) {
        int x;
        cin >> x;
        b.pb(x - i - 1);
    }
    b.pb(l - n);

/*    for (int x: a) cerr << x << ' ';
    cerr << '\n';
    for (int x: b) cerr << x << ' ';
    cerr << '\n';*/

    bool ok = true;
    for (int x: b) {
        ok &= *lower_bound(all(a), x) == x;
    }
    if (!ok) cout << -1 << '\n';
    else {
        i64 ans = 0;
        forn(i, n + 2) {
            if (i + 1 < n + 2 && b[i] == b[i + 1]) continue;
            auto w1 = irange(a, b[i]);
            auto w2 = irange(b, b[i]);
            if (w1.fi > w2.fi) ans += w1.fi - w2.fi;
            if (w1.se < w2.se) ans += w2.se - w1.se;
        }
        cout << ans << '\n';
    }

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define oo 1000000010
const int N = 100010;
int n , l , a[N] , b[N];

map < int , int > mp , mp2;


int in[N] , it , val , g[N];

/*

pair< int , int > seg[4 * N];

void update(int s,int e,int idx){
	if(s > r || e < l)
		return;
	if(s >= l && e <= r){
		seg[idx] = make_pair(it , val);
		return;
	}
	update(s , ((s+e) >> 1),(idx << 1));
	update(((s+e) >> 1) + 1,e,(idx << 1) + 1);
}

int get(int i){
	int s = 0 , e = n - 1 , idx = 1;
	pair< int , int > ret = make_pair(-1 , -1);
	while(true){
		ret = max(ret , seg[idx]);
		if(s == e)
			break;
		if(i <= ((s + e) >> 1)){
			idx = (idx << 1);
			e = ((s + e) >> 1);
		}
		else{
			idx = (idx << 1) + 1;
			s = ((s + e) >> 1) + 1;
		}
	}
	return ret.second;
}

*/
int main(){
	scanf("%d%d",&n,&l);
	n += 2;
	a[0] = b[0] = 0;
	a[n - 1] = b[n - 1] = l + 1;
	for(int i = 1 ;i < n - 1;i++) scanf("%d",&a[i]);
	for(int i = 1 ;i < n - 1;i++) scanf("%d",&b[i]);
	for(int i = 0 ;i < n;i++){
		if(mp.find(a[i] - i) == mp.end())
			mp[a[i] - i] = i;
		mp2[a[i] - i] = i;
		g[i] = i;
	}
	int cur , mn  , mx;
	for(int i = 1 ;i < n - 1;i++){
		if(b[i] == a[i]) continue;
		if(mp.find(b[i] - i) == mp.end()){puts("-1");return 0;}
		mx = mp2[b[i] - i];
		mn = mp[b[i] - i];
		if(i < mn)
			g[i] = mn;
		else
			g[i] = mx;
	}
	long long ans = 0;
	int j , last;
	for(int i = 0 ;i < n;i++){
		if(g[i] == i) continue;
		if(g[i] < i){
			j = i;
			while(j < n && g[j] < j)
				j++;
			j--;
			last = j;
			for(int k = j ;k >= i;k--){
				if(last == g[k]) continue;
				ans += k - g[k];
				last = g[k];
			}
			i = j;
			continue;
		}
		last = i;
		while(i < n && g[i] > i){
			if(last == g[i]){
				i++;
				continue;
			}
			ans += g[i] - i;
			last = g[i];
			i++;
		}
		i--;
	}
	cout << ans << endl;
	return 0;
}