#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using pii = pair<int,int>;

void debug(map<int,pii> a) {
  for (auto p : a) {
    cout << p.first << ": " << "(" << p.second.first << "," << p.second.second << ")\n";
  }
}

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

  int N,L;
  cin >> N >> L;

  auto read = [](int N, int L) {
    vector<int> v{0};
    for (int i = 1; i <= N+1; i++) {
      int ai = L+1;
      if (i <= N)
        cin >> ai;
      v.push_back(ai-i);
    }
    map<int,pii> interval;
    for (int i = 0; i < v.size(); i++) {
      if (i==0 || v[i] != v[i-1])
        interval[v[i]].first = i;
      interval[v[i]].second = i;
    }
    return interval;
  };
  auto A = read(N,L);
  auto B = read(N,L);
  ll ans = 0;
  for (auto p : B) {
    int val = p.first;
    if (!A.count(val)) {
      cout << -1 << '\n';
      return 0;
    }
    ans += max(A[val].first-B[val].first,0) + max(B[val].second-A[val].second,0);
  }
  cout << ans << '\n';
}


#include <bits/stdc++.h>
typedef long long int64;
typedef unsigned int uint;
typedef unsigned long long uint64;

const int N = 1e5;
int n, L, a[N + 5], b[N + 5];

int main() {
	scanf("%d%d", &n, &L);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	for (int i = 1; i <= n; i++) {
		scanf("%d", &b[i]);
	}
	a[n + 1] = b[n + 1] = L + 1;
	for (int i = n + 1; i >= 1; i--) {
		a[i] -= a[i - 1] + 1;
		b[i] -= b[i - 1] + 1;
	}
	int64 ans = 0;
	for (int j = 1, i = 1; i <= n + 1; i++) {
		if (b[i] > 0) {
			for (; a[j] == 0; j++);
			ans += std::max(0, i - j);
			int sum = 0;
			for (; j <= n + 1 && sum < b[i]; sum += a[j++]);
			if (sum != b[i]) {
				printf("-1\n");
				return 0;
			}
			ans += std::max(0, j - 1 - i);
		}
	}
	printf("%lld\n", ans);
	return 0;
}