#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout.setf(ios::fixed); cout.precision(20); 
    #endif
    int n, L;
    cin >> n >> L;
    vector <int> a(n + 2), b(n + 1);
    a[n + 1] = L + 1;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }   
    for (int i = 1; i <= n; ++i)
        cin >> b[i];

    map <int, int> l, r;
    for (int i = 0; i <= n + 1; ++i) {
        if (!l.count(a[i] - i)) {
            l[a[i] - i] = i;
        }
        r[a[i] - i] = i;
    }   

    vector <int> link(n + 1);
    int ans = 0;
    for (int i = 1; i <= n; ++i) {
        if (a[i] == b[i]) {
            link[i] = -1;
        }   
        else if (b[i] < a[i]) {
            if (!r.count(b[i] - i)) {
                cout << -1 << endl;
                exit(0);
            }   

            link[i] = r[b[i] - i];
        }   
        else {
            if (!l.count(b[i] - i)) {
                cout << -1 << endl;
                exit(0);
            }   

            link[i] = l[b[i] - i];
        }   
    }   

    #ifdef HOME
    for (int i = 1; i <= n; ++i) {
        cout << link[i] << ' ';
    }   
    cout << endl;
    #endif

    int mx = 0;
    for (int i = 1; i <= n; ++i) {
        if (link[i] != -1 && link[i] > i) {
            if (link[i] < mx) {
                cout << -1 << endl;
                exit(0);
            }   
            if (link[i] > mx) {
                ans += link[i] - i;
                mx = link[i];
            }   
        }
    }   

    #ifdef HOME
    cout << "right ok" << endl;
    debug(ans);
    #endif

    const int INF = 1e18;
    int mn = INF;
    for (int i = n; i; --i) {
        if (link[i] != -1 && link[i] < i) {
            if (link[i] > mn) {
                cout << -1 << endl;
                exit(0);
            }   
            if (link[i] < mn) {
                ans += i - link[i];
                mn = link[i];
            }   
        }   
    }

    cout << ans << endl;
}
#include<bits/stdc++.h>
#define pb emplace_back
#define AI(i) begin(i), end(i)
using namespace std;
using ll = long long;
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() {cerr << endl;}
template<class T1, class ...T2>
void kout (T1 v, T2 ...e) { cerr << v << ' ', kout(e...); }
template<class T>
void debug(T L, T R) { while (L != R) cerr << *L << " \n"[next(L)==R], ++L; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 300010;
int n, L, a[MAX_N], b[MAX_N], c[MAX_N], d[MAX_N];
ll solve() {
	for (int i = 1;i <= n;++i) {
		c[i] = a[i] - a[i-1] - 1;
		d[i] = b[i] - b[i-1] - 1;
	}
	ll res = 0;
	for (int i = 1, j = 1;i <= n;++i) {
		if (d[i] == 0) continue;
		while (!c[j])
			++j;
		int k = j, sum = 0;
		while (sum < d[i]) {
			assert(k <= n);
			sum += c[k++];
		}
		if (sum != d[i]) 
			return -1;

		res += max(0, k-i-1) + max(0, i-j);
		j = k;
	}
	return res;
}
signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n >> L;
	for (int i = 1;i <= n;++i)
		cin >> a[i];
	for (int i = 1;i <= n;++i)
		cin >> b[i];
	a[n+1] = b[n+1] = L+1, ++n;
	cout << solve() << '\n';
}

