#ifdef Rahul
#include "RAHUL.h"
#else
#include <bits/stdc++.h>
using namespace std;
#define error(...) 42;
#endif
#define SZ(v) int((v).size())
#define ALL(vec) begin(vec), end(vec)
typedef long long i64;
template<typename T> inline bool uax(T &x, T y) {return (y > x) ? x = y, true : false;}
template<typename T> inline bool uin(T &x, T y) {return (y < x) ? x = y, true : false;}
template<typename T> void kek(T ans) {cout << ans << endl; exit(0);}
#define Luv(...) [&] (auto &&u, auto &&v) { return __VA_ARGS__; }
const i64 INF = (i64) 4e18 + 42;
const int MOD = (int) 1e9 + 7;

int main() {
  cin.tie(nullptr) -> sync_with_stdio(false);
  
  int n, k; cin >> n >> k;
  vector<int> a(n), b(n);
  for (int &x : a) cin >> x;
  for (int &x : b) cin >> x;
  int m = k - n;
  set<int> sa{0, m};
  for (int i = 0; i < n; ++i) {
    a[i] -= i + 1;
    b[i] -= i + 1;
    sa.insert(a[i]);
  }
  error(sa);
  error(a);
  error(b);
  int ok = 1;
  for (int x : b) {
    ok &= (int) sa.count(x);
  }
  if (!ok) kek(-1);
  a.insert(begin(a), 0);
  b.insert(begin(b), 0);
  a.push_back(m);
  b.push_back(m);
  i64 ans = 0;
  int nx = -1, val = -1;
  for (int i = 1, j = 1; i <= n; ++i) {
    if (nx >= i) a[i] = val;
    if (a[i] < b[i]) {
      j = i;
      while (1) {
        if (nx >= j) a[j] = val;
        if (a[j] >= b[i]) break;
        ++j;
      }
      nx = j;
      val = a[j];
      a[i] = val;
      ans += j - i;
      if (a[j] != b[i]) kek(-1);
    }
    error(i, a[i], b[i], ans);
  }
  error(a);
  error(b);
  for (int &x : a) x *= -1;
  for (int &x : b) x *= -1;
  reverse(ALL(a));
  reverse(ALL(b));
  nx = -1, val = -1;
  for (int i = 1, j = 1; i <= n; ++i) {
    if (nx >= i) a[i] = val;
    if (a[i] < b[i]) {
      j = i;
      while (1) {
        if (nx >= j) a[j] = val;
        if (a[j] >= b[i]) break;
        ++j;
      }
      nx = j;
      val = a[j];
      a[i] = val;
      ans += j - i;
      if (a[j] != b[i]) kek(-1);
    }
    error(i, a[i], b[i], ans);
  }
  error(a);
  error(b);
  kek(ans);
}

#include <bits/stdc++.h>
using namespace std;
#define per(i,a,b) for(int i=(b)-1;i>=a;i--)
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define de(x) cout<<#x<<" = "<<x<<"\n"
#define dd(x) cout<<#x<<" = "<<x<<" "
#define mes(p) memset(p,0,sizeof(p))
#define all(x) x.begin(),x.end()
#define rson mid+1,r,rt<<1|1
#define sz(x) (int)x.size()
#define lson l,mid,rt<<1
#define mp make_pair 
#define pb push_back
#define ls (rt<<1)
#define rs (ls|1)
#define se second
#define fi first
typedef pair <int ,int > pii;
typedef vector <int > vi;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f; 
const int mod = 1e9+7;
 
const int maxn= 1e5+5;
const int N = 1e6 + 6;
int a[maxn], b[maxn], c[maxn];
int main(){
	int n, L; 
	scanf("%d%d", &n, &L);
	rep(i,1,n+1) scanf("%d", &a[i]);
	rep(i,1,n+1) scanf("%d", &b[i]);
	a[n+1] = b[n+1] = L + 1;
	rep(i,0,n+2) c[i] = a[i] - i;// dd(i),de(c[i]);
	int f = 1;
	ll ans = 0;
	rep(i,1,n+1){
		if(a[i] == b[i]) continue;
		if(b[i] == b[i-1] + 1 || b[i] == a[i-1] + 1) 
			ans += (b[i-1] < a[i]);
		else {
			int k = lower_bound(c, c + n + 2, b[i] - i) - c;
			if(k < i) k = upper_bound(c, c+n+2, b[i]-i) - c - 1;
			if(0 <= k && k < n + 2 && c[k] == b[i] - i)
				ans += abs(k - i);
			else f = 0;
	//		dd(i), de(k);
		}
	//	dd(i), de(ans);
		//a[i] = b[i];
	}
	if(!f) ans = -1;
	cout << ans;
    return 0; 
}
/*
10 1000
1 3 5 7 9 11 13 15 17 19
6 8 10 12 14 15 16 998 999 1000
10 1000
2 4 6 8 10 12 14 16 18 20
1 2 3 4 5 7 9 11 13 15
10 100
2 3 4 5 6 7 8 9 10 11
1 2 3 4 5 6 7 8 9 10
10 1000
1 3 5 7 9 11 13 15 17 19
6 7 8 9 10 14 15 16 998 999 1000
*/
