#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
 public:
  LL n;
  LL l;
  vector<LL> a, b, A, B;
  Solver(LL n) : n(n), a(n + 2), b(n + 2), A(n + 1), B(n + 1){};

  void solve() {
    cin >> l;
    for (int i = 0; i < n; ++i) {
      cin >> a[i + 1];
    }
    for (int i = 0; i < n; ++i) {
      cin >> b[i + 1];
    }
    a[n + 1] = b[n + 1] = l + 1;
    for (int i = 0; i <= n; ++i) {
      A[i] = a[i + 1] - a[i] - 1;
      B[i] = b[i + 1] - b[i] - 1;
    }
    LL ans = 0;
    LL nxt = 0;
    DBG(A, B)
    for (LL i = 0; i <= n; ++i) {
      if (B[i] == 0) continue;
      LL j = nxt;
      LL tmpsum = 0;
      for (; j <= n; ++j) {
        tmpsum += A[j];
        if (tmpsum == B[i]) break;
      }
      DBG(j)
      if (j > n) {
        cout << -1 << endl;
        return;
      }
      DBG(i, j)
      int l = nxt;
      int r = j;
      while (A[l] == 0) l++;
      while (A[r] == 0) r--;
      //[nxt,j]
      if (i < l) {
        ans += r - i;
      } else if (i > r) {
        ans += i - l;
      } else {
        ans += r - l;
      }
      nxt = j + 1;
      DBG(ans)
    }
    cout << ans << endl;
  }
};

}  // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}

///****In the name of ALLAH, most Gracious, most Compassionate****//

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

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

typedef long long LL;
typedef pair <LL, LL> pii;

#define ALL(a) a.begin(), a.end()
#define FastIO ios::sync_with_stdio(false); cin.tie(0);cout.tie(0)
#define IN freopen("input.txt","r+",stdin)
#define OUT freopen("output.txt","w+",stdout)

#define DBG(a) cerr<< "line "<<__LINE__ <<" : "<< #a <<" --> "<<(a)<<endl
#define NL cout<<endl

template < class T1,class T2>
ostream &operator <<(ostream &os,const pair < T1,T2 > &p)
{
    os<<"{"<<p.first<<","<<p.second<<"}";
    return os;
}

const LL N=3e5+5;
const LL oo=1e9+7;
vector<LL > ai,bi;
int32_t main()
{
    FastIO;
    map<LL  ,LL > alo,ahi;
    map<LL  ,LL > blo,bhi;

    LL n,L;
    cin>>n>>L;
    for(LL i=1;i<=n;i++)
    {
        LL x;
        cin>>x;
        x-=i;

        if(alo.count(x))
            ahi[x] = i;
        else
            alo[x] =ahi[x] = i;
    }
    for(LL i=1;i<=n;i++)
    {
        LL x;
        cin>>x;
        x-=i;

        if(blo.count(x))
            bhi[x] = i;
        else
            blo[x] =bhi[x] = i;
    }

    alo[0]=0;
    ahi[0]=max(ahi[0],0LL);

    blo[0]=0;
    bhi[0]=max(bhi[0],0LL);

    L-=n+1-1;

    ahi[L] = n+1;
    if(!alo.count(L))
        alo[L]=n+1;

    bhi[L] = n+1;
    if(!blo.count(L))
        blo[L]=n+1;

    LL ans = 0;

//    DBG(&alo);
//    for(auto i: alo)
//        cout<<i<<" ";
//    NL;

    for(auto  it: blo)
    {
        LL x = it.first;
        LL l = blo[x];
        LL r = bhi[x];
//        DBG(it);

        if(alo.count(x) == 0)
        {
            cout<<"-1\n";
            return 0;
        }

        LL ll = alo[x];
        LL rr = ahi[x];

        ans += max( 0LL, ll-l);
        ans += max( 0LL, r-rr);

    }
    cout<<ans<<"\n";
}
