#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

typedef long double ld;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const long long infll = (long long) 1.01e18;
const ld eps = 1e-9;
const ld pi = acos((ld) -1);

#ifdef DEBUG
mt19937 mrand(300); 
#else
mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); 
#endif

int rnd(int x) {
  return mrand() % x;
}

void precalc() {
}

const int maxn = (int) 1e5 + 5;
int n, l;
int a[maxn], b[maxn];

bool read() {
  if (scanf("%d%d", &n, &l) < 2) {
    return false;
  }
  l -= n - 1;
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    a[i] -= i + 1;
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", &b[i]);
    b[i] -= i + 1;
  }
  return true;
}

void solve() {
  for (int i = 0; i < n; ++i) {
    if (b[i] == 0 || b[i] == l - 1) {
      continue;
    }
    int pos = lower_bound(a, a + n, b[i]) - a;
    if (pos >= n || a[pos] != b[i]) {
      printf("-1\n");
      return;
    }
  }
  long long res = 0;
  for (int i = 0; i < n; ++i) {
    if (b[i] == a[i]) {
      continue;
    }
    if (b[i] < a[i]) {
      if (b[i] == 0 || (i && b[i] == b[i - 1])) {
        ++res;
        continue;
      }
      int pos = upper_bound(a, a + n, b[i]) - a - 1;
      res += i - pos;
    } else {
      if (b[i] == l - 1 || (i + 1 < n && b[i] == b[i + 1])) {
        ++res;
        continue;
      }
      int pos = lower_bound(a, a + n, b[i]) - a;
      res += pos - i;
    }
  }
  printf("%lld\n", res);
}

int main() {
  precalc();
#ifdef DEBUG
  assert(freopen(TASK ".in", "r", stdin));
  assert(freopen(TASK ".out", "w", stdout));
#endif
  while (read()) {
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;

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

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]);

  ++n;
  a[n] = b[n] = L + 1;
  ++n;

  for (int i = 0; i < n; ++i) {
    a[i] -= i, b[i] -= i;
  }

  int l1 = 0, l2 = 0;
  ll ans = 0;
  while (l1 < n && l2 < n) {
    int r2 = l2 + 1;
    while (r2 < n && b[r2] == b[l2])
      ++r2;
    while (l1 < n && a[l1] < b[l2])
      ++l1;
    if (l1 >= n || a[l1] != b[l2]) {
      puts("-1");
      exit(0);
    }
    int r1 = l1 + 1;
    while (r1 < n && a[r1] == a[l1])
      ++r1;
    r1--, r2--;
    if (l2 < l1) ans += l1 - l2;
    if (r2 > r1) ans += r2 - r1;
    l1 = r1 + 1;
    l2 = r2 + 1;
  }
  printf("%lld\n", ans);
}
