#include <cstdio>
#include <iostream>
#include <cassert>
#include <string>
#include <algorithm>
#include <cstring>
#include <utility>
#include <tuple>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <random>
#include <chrono>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)

using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int main() {
    int N;
    scanf("%d", &N);
    vector<int> A(N, 0);
    for (int i = 0; i < N; i++)
        scanf("%d", &A[i]);
    vector<LL> psum(N, 0);
    for (int i = 0; i < N; i++) {
        if (i) psum[i] = psum[i - 1];
        int offs = 1;
        if (i % 2) offs = -1;
        psum[i] += offs * A[i];
    }
    map<LL, int> cnt;
    cnt[0] = 1;
    LL ans = 0;
    for (int i = 0; i < N; i++) {
        LL cur = psum[i];
        ans += cnt[cur];
        cnt[cur]++;
    }
    printf("%lld\n", ans);

    return 0;
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
    * DON'T GET STUCK ON ONE APPROACH
by Benq;
*/

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define ti3 tuple<int,int,int>
#define mat vector<vector<int>>
const int inf = 1 << 30;
const ll mod = 1e9 + 7;
const ll linf = 1LL << 62;
const db EPS = 1e-7;
template<class T> void chmin(T& x, T y){if(x > y) x = y;}
template<class T> void chmax(T& x, T y){if(x < y) x = y;}

ll ans, N, L;
vector<int> A, B;

int main() {
    cin >> N >> L;
    int pre = 0;
    for (int i = 0; i < N; i++) {
        int a;
        cin >> a;
        A.push_back(a - pre - 1);
        pre = a;
    }
    A.push_back(L - pre);
    A.push_back(0);
    pre = 0;
    for (int i = 0; i < N; i++) {
        int b;
        cin >> b;
        B.push_back(b - pre - 1);
        pre = b;
    }
    B.push_back(L - pre);
    int idx = 0, l = 0, r = 0;
    for (int i = 0; i < N + 1; i++) {
        if (B[i] == 0) continue;
        while (A[idx] == 0) idx++;
        l = idx, r = idx;
        int sum = 0;
        for (; idx < N + 2; idx++) {
            if (sum >= B[i]) break;
            sum += A[idx];
            r = idx;
        }
        if (sum != B[i]) { 
            cout << -1 << endl;
            return 0;
        }
        ans += max(i - l, 0) + max(r - i, 0); 
    }
    cout << ans << endl;
}