#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
const int INF = 1 << 29;
const int MOD = 1e9 + 7;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n + 2), b(n + 2);
  a[0] = 0; a[n + 1] = m + 1;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  map<int, int> mi, mx;
  for (int i = 0; i <= n + 1; ++i) {
    int x = a[i] - i;
    if (mi.find(x) == mi.end()) {
      mi[x] = i;
    } else {
      mi[x] = min(mi[x], i);
    }
    if (mx.find(x) == mx.end()) {
      mx[x] = i;
    } else {
      mx[x] = max(mx[x], i);
    }
  }
  // trace(mi, mx);
  vector<int> left(n + 2, INF), right(n + 2, -INF);
  bool found = false;
  for (int i = 1; i <= n; ++i) {
    cin >> b[i];
    int x = b[i] - i;
    if (mi.find(x) == mi.end()) {
      found = true;
      continue;
    }
    int L = mi[x], R = mx[x];
    if (i >= L && i <= R) continue;
    if (i < L) {
      left[L] = min(left[L], i);
    } else {
      right[R] = max(right[R], i);
    }
  }
  if (found) {
    cout << -1 << '\n';
    return 0;
  }

  int64 ret = 0;
  for (int i = 0; i <= n + 1; ++i) {
    // trace(i, left[i], right[i]);
    ret += max(0, i - left[i]);
    ret += max(0, right[i] - i);
  }
  cout << ret << '\n';

  return 0;
}

#include <bits/stdc++.h>
using Int = long long;  // clang-format off
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define ALL(v) std::begin(v), std::end(v)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif  // clang-format on
struct in {
    template <class T> operator T() {
        T t;
        std::cin >> t;
        return t;
    }
};
void out() { std::cout << "\n"; }
template <class Head, class... Tail> void out(Head&& h, Tail&&... t) {
    std::cout << h << (sizeof...(Tail) == 0 ? "" : " "), out(std::forward<Tail>(t)...);
}

/**
 *    author:  knshnb
 *    created: Sun Oct 18 22:11:15 JST 2020
 **/

template <class T> bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; }
template <class T> bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; }

/// @docs src/Helper/BinarySearch.md
template <class F> long long binary_search(long long ok, long long ng, F check) {
    while (std::abs(ok - ng) > 1) {
        long long mid = (ok + ng) / 2;
        (check(mid) ? ok : ng) = mid;
    }
    return ok;
}

void no() {
    out(-1);
    exit(0);
}
signed main() {
    Int n = in(), L = in();
    std::vector<Int> a(n), b(n);
    REP(i, n) a[i] = in();
    REP(i, n) b[i] = in();
    std::vector<Int> xs = {0};
    for (Int x : a) xs.push_back(x);
    xs.push_back(L + 1);
    std::vector<Int> vec(n);
    REP(i, n) {
        if (a[i] > b[i]) {
            Int idx = binary_search(0, i + 1, [&](Int mid) { return xs[mid] + i + 1 - mid <= b[i]; });
            if (xs[idx] + i + 1 - idx != b[i]) no();
            vec[i] = idx;
        } else if (a[i] < b[i]) {
            Int idx = binary_search(n + 1, i + 1, [&](Int mid) { return xs[mid] - (mid - (i + 1)) >= b[i]; });
            if (xs[idx] - (idx - (i + 1)) != b[i]) no();
            vec[i] = idx;
        } else {
            vec[i] = i + 1;
        }
    }
    dump(vec);
    REP(i, n - 1) {
        if (vec[i] > vec[i + 1]) no();
    }
    std::vector<Int> pma(n + 2), nma(n + 2);
    REP(i, n) {
        Int dif = vec[i] - (i + 1);
        if (dif > 0) chmax(pma[vec[i]], dif);
        if (dif < 0) chmax(nma[vec[i]], -dif);
    }
    out(std::accumulate(ALL(pma), 0LL) + std::accumulate(ALL(nma), 0LL));
}
