#include<bits/stdc++.h>
using namespace std;
int N, L, A[100010], B[100010]; 
long long ans; 
vector<int> E;
bool seen[100010], added[100010];  
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> N >> L; 
	A[0] = 0; B[0] = 0; 
	A[N+1] = L+1; B[N+1] = L+1; 
	for(int i = 1; i <= N; i++) cin >> A[i]; 
	for(int i = 1; i <= N; i++) cin >> B[i];  
	for(int i = 0; i <= N+1; i++){
		if(A[i] == B[i]) E.push_back(i); 
	} 
	for(int e : E){
		seen[e] = true; 
		int l = e; int r = e;
		for(int i = e+1; i <= L+1; i++){
			if(A[i] <= B[i]) break; 
			r = i; seen[i] = true; 
		}
		for(int i = e-1; i >= 0; i--){
			if(A[i] >= B[i]) break; 
			l = i; seen[i] = true;
		}
		int j = l; 
		for(int i = l; i <= e; i++){
			while(A[j] - j + i != B[i]){
				j++; 
				if(j > e){
					cout << -1 << "\n"; return 0; 
				}
			}
			if(!added[j]) ans += j-i; 
			added[j] = true; 
		}
		for(int i = l; i <= e; i++) added[i] = false; 
		j = r; 
		for(int i = r; i >= e; i--){
			while(A[j] + i - j != B[i]){
				j--; 
				if(j < e){
					cout << -1 << "\n"; return 0; 
				}
			}
			if(!added[j]) ans += i-j; 
			added[j] = true;
		}
		for(int i = r; i >= e; i--) added[i] = false;
	}
	for(int i = 0; i <= N+1; i++){
		if(!seen[i]){
			cout << -1 << "\n"; return 0; 
		}
	}
	cout << ans << "\n"; 
}
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, a, b) for (int (i) = (a); (i) <= (b); (i)++)
#define ROF(i, a, b) for (int (i) = (a); (i) >= (b); (i)--)
#define REP(i, n) FOR(i, 0, (n)-1)
#define sqr(x) ((x) * (x))
#define all(x) (x).begin(), (x).end()
#define reset(x, y) memset(x, y, sizeof(x))
#define uni(x) (x).erase(unique(all(x)), (x).end())
#define BUG(x) cerr << #x << " = " << (x) << endl
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define _1 first
#define _2 second
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)

const int maxn = 112345;

int n, L;
int a[maxn], b[maxn];
bool vis[maxn];

int main() {
  scanf("%d%d", &n, &L);
  FOR(i, 1, n) scanf("%d", a + i);
  FOR(i, 1, n) scanf("%d", b + i);
  a[n + 1] = b[n + 1] = L + 1;
  ll ans = 0;
  FOR(i, 1, n) if (a[i] == b[i]) vis[i] = true;
  map<int, int> occ;
  occ[0] = 0;
  FOR(i, 1, n) {
    occ[a[i] - i] = i;
    if (b[i] < a[i] && occ.count(b[i] - i)) {
      int hedge = i - occ[b[i] - i];
      if (hedge <= 1) ans += hedge;
      else if (b[i - 1] == b[i] - 1) ans++;
      else ans += hedge;
      vis[i] = true;
    }
  }
  occ.clear();
  occ[a[n + 1] - n - 1] = n + 1;
  ROF(i, n, 1) {
    occ[a[i] - i] = i;
    if (b[i] > a[i] && occ.count(b[i] - i)) {
      int hedge = occ[b[i] - i] - i;
      if (hedge <= 1) ans += hedge;
      else if (b[i + 1] == b[i] + 1) ans++;
      else ans += hedge;
      vis[i] = true;
    }
  }
  FOR(i, 1, n) if (!vis[i]) {
    puts("-1");
    return 0;
  }
  printf("%lld", ans);
}
