#include<bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i,m,n) for(int i=(m);i<(n);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define all(a) a.begin(),a.end()
#define rall(c) (c).rbegin(),(c).rend()
#define mp make_pair
#define endl '\n'
//#define vec vector<ll>
//#define mat vector<vector<ll> >
#define fi first
#define se second
#define double long double
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
//typedef long double ld;
typedef complex<double> Complex;
const ll INF=1e9+7;
const ll MOD=998244353;
const ll inf=INF*INF;
const ll mod=MOD;
const ll MAX=1000010;
const double PI=acos(-1.0);
typedef vector<vector<ll> > mat;
typedef vector<ll> vec;

//dijkstra(O(ElogV))
//0-indexed
void dijkstra(ll n,ll s,vector<vector<pair<ll,pll> > >&G,vector<ll> &d){
    priority_queue<pll,vector<pll>,greater<pll> >pq;
    rep(i,n){
        d[i]=inf;
    }
    d[s]=0;
    pq.push(mp(0,s));
    while(!pq.empty()){
        pll k=pq.top();pq.pop();
        ll v=k.second;
        if(d[v]<k.first)continue;
        for(auto e:G[v]){
            ll x=e.se.fi,y=e.se.se;
            ll nxt=x+y/(d[v]+1);
            if(y>0){
            ll z=sqrt(y);
                if(z>=d[v]){
                    nxt=min(nxt,x+min(y/(z+1)+z-d[v],y/(z+2)+1+z-d[v]));
                }
                if(z-1>=d[v]){
                    nxt=min(nxt,x+y/z+z-1-d[v]);
                }
            }
            if(d[e.first]>d[v]+nxt){
                d[e.first]=d[v]+nxt;
                pq.push(mp(d[e.first],e.first));
            }
        }
    }
}


signed main(){
    ll n,m;cin>>n>>m;
    vector<vector<pair<ll,pll> > >G(n);
    rep(i,m){
        ll a,b,c,d;cin>>a>>b>>c>>d;
        a--;b--;
        G[a].pb(mp(b,mp(c,d)));
        G[b].pb(mp(a,mp(c,d)));
    }
    vector<ll>d(n);
    dijkstra(n,0,G,d);
    ll ans=d[n-1];
    if(ans==inf)ans=-1;
    cout<<ans<<endl;
}
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+100;
typedef long long ll;
const int inf = 0x3f3f3f3f;
typedef pair<int,int> pii;
#define fr(i,a,b) for(int i = a;i <= b; ++i)
#define nfr(i,a,b) for(int i = a;i >= b; --i)

int n,l;
int a[N],b[N];
int pos[N];
set<pii> s1;
int main(){
   scanf("%d%d",&n,&l);
   fr(i,1,n)scanf("%d",a+i);
   fr(i,1,n)scanf("%d",b+i);
   fr(i,1,n){
      s1.insert(pii(a[i]-i,i));
   }
   a[n+1] = l+1;
   s1.insert(pii(0,0)); s1.insert(pii(a[n+1]-n-1,n+1));

   ll ans = 0;
   bool flag = true;
   fr(i,1,n){
      if(b[i] <= a[i]){
         pii cj = *(--s1.upper_bound(pii(b[i]-i,i)));
         if(cj.first != b[i]-i){
            flag = false;
         } else pos[i] = cj.second;
      } else {
         pii cj = *s1.lower_bound(pii(b[i]-i,i));
         if(cj.first != b[i]-i){
            flag = false;
         } else pos[i] = cj.second;
      }
   }
   if(!flag){
      puts("-1"); return 0;
   }

   pos[n+1] = pos[0] = 1145141919;
   fr(i,1,n){
      if(pos[i] == i)continue ;
      if(pos[i] < i){
         if(pos[i] == pos[i+1])continue ;
         else ans += i-pos[i];
      } else {
         if(pos[i] == pos[i-1])continue ;
         else ans += pos[i]-i;
      }
   }

   printf("%lld\n",ans);
   return 0;
}
