#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <functional>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <climits>
#include <queue>
#include <bitset>
#include <math.h>
using namespace std;
#define MOD 1000000007LL
#define LL long long

int n, L;

vector<int> convert(vector<int> N) {
    vector<int> R;
    int lx = 0;
    for (const auto &x : N) {
        R.push_back(x - lx - 1);
        lx = x;
    }
    R.push_back(L - lx);
    return R;
}

LL solve() {
    cin >> n >> L;
    vector<int> A(n);
    vector<int> B(n);
    for (int i = 0 ; i < n; i++) cin >> A[i];
    for (int i = 0 ; i < n; i++) cin >> B[i];
    A = convert(A);
    B = convert(B);

    int j = 0;
    LL sol = 0;
    for (int i = 0 ; i < B.size(); i++) {
        int sa = 0;
        int m1 = i;
        int m2 = i;
        while (sa < B[i] && j < A.size()) { 
            sa += A[j];
            if (A[j] > 0) {
                m1 = min(m1, j);
                m2 = max(m2, j);
            }
            j++;
        }
        sol += m2 - m1;
        if (B[i] != sa) return -1;
    }    
    return sol;
}

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

    int test = 1; //cin >> test;
    while(test--) {
        cout << solve() << endl;
    }    

    return 0;
}
#include <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <cstring>
#include <list>  
#include <cassert>
#include <climits>
#include <bitset>
#include <chrono>
#include <random>
using namespace std;

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
std::mt19937 rnd((int)std::chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }

const int MAXN = 100000 + 2;

int n, len;
int a[MAXN];
int b[MAXN];

map<int, pair<int, int>> awho;

int target[MAXN];

ll solve() {
	//printf("a':"); REP(i, n) printf(" %d", a[i] - i); puts("");
	//printf("b':"); REP(i, n) printf(" %d", b[i] - i); puts("");
	REP(i, n) {
		int x = a[i] - i;
		if (!awho.count(x)) { awho[x] = MP(i, i); continue; }
		assert(awho[x].second == i - 1);
		++awho[x].second;
	}
	REP(i, n) {
		if (!awho.count(b[i] - i)) return -1;
		pair<int, int> cur = awho[b[i] - i];
		if (i < cur.first) target[i] = cur.first; else if (i > cur.second) target[i] = cur.second; else target[i] = i;
	}

	ll ret = 0;
	{
		int at = 0;
		while (at < n) {
			if (target[at] <= at) { ++at; continue; }
			ret += target[at] - at;
			int to = at; while (to < n && target[to] == target[at]) ++to;
			at = to;
		}
	}
	{
		int at = n - 1;
		while (at >= 0) {
			if (target[at] >= at) { --at; continue; }
			ret += at - target[at];
			int to = at; while (to >= 0 && target[to] == target[at]) --to;
			at = to;
		}
	}

	return ret;
}


void run() {
	scanf("%d%d", &n, &len);
	n += 2;
	a[0] = b[0] = 0, a[n - 1] = b[n - 1] = len + 1;
	FOR(i, 1, n - 1) scanf("%d", &a[i]);
	FOR(i, 1, n - 1) scanf("%d", &b[i]);
	printf("%lld\n", solve());
}

int main() {
	run();
	return 0;
}
