#ifdef xay5421
#define D(...) fprintf(stderr,__VA_ARGS__)
#else
#define D(...) ((void)0)
#define NDEBUG
#endif
#include<bits/stdc++.h>
#define int long long
#define LL long long
#define MP make_pair
#define PB push_back
#define X first
#define Y second
#define SZ(x) ((int)(x).size())
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef pair<int,int>PII;typedef vector<int>VI;typedef vector<PII>VII;
template<typename T>void rd(T&x){int f=0,c;while(!isdigit(c=getchar()))f^=!(c^45);x=(c&15);while(isdigit(c=getchar()))x=x*10+(c&15);if(f)x=-x;}
template<typename T>void pt(T x,int c=-1){if(x<0)putchar('-'),x=-x;if(x>9)pt(x/10);putchar(x%10+48);if(c!=-1)putchar(c);}
const int N=100005;
int n,L,a[N],b[N],ans;
vector<int>G[N];
map<int,int>lst,sum;
void sol(){
	lst.clear(),sum.clear(),lst[0]=0;
	rep(i,1,n)a[i]-=i,b[i]-=i;
	rep(i,1,n){
		lst[a[i]]=i;
		if(b[i]<a[i]){
			if(!lst.count(b[i]))puts("-1"),exit(0);
			ans+=i-lst[b[i]];
			ans-=sum[b[i]];
			sum[b[i]]=i-lst[b[i]];
		}
	}
	rep(i,1,n)a[i]+=i,b[i]+=i;
}
signed main(){
	rd(n),rd(L);
	rep(i,1,n)rd(a[i]);
	rep(i,1,n)rd(b[i]);
	sol();
	rep(i,1,n)a[i]=L-a[i]+1,b[i]=L-b[i]+1;
	reverse(a+1,a+1+n);
	reverse(b+1,b+1+n);
	sol();
	pt(ans,'\n');
	return 0;
}

#include <iostream>
#include <vector>
#include <queue>

using pli = std::pair<int64_t, int>;

struct Edge {
  Edge(int to, int t_p, int c) : to(to), t_point(t_p), cost(c) {}
  int to, t_point, cost;
};

int64_t earliest_time(const std::vector<std::vector<Edge>>& graph,
                  int n, int from, int to) {
  std::priority_queue<pli, std::vector<pli>, std::greater<pli>> que;
  std::vector<bool> used(n + 1, false);
  que.emplace(0, from);
  while (!que.empty()) {
    pli cur = que.top();
    que.pop();
    int64_t arrival = cur.first;
    int v = cur.second;
    if (v == to) return arrival;
    if (used[v]) continue;
    used[v] = true;
    for (const auto& edge : graph[v]) {
      if (used[edge.to]) continue;
      int64_t leave = (arrival + edge.t_point - 1) / edge.t_point * edge.t_point;
      que.emplace(leave + edge.cost, edge.to);
    }
  }
  return -1;
}

int main() {
  int n, m, from, to;
  std::cin >> n >> m >> from >> to;
  std::vector<std::vector<Edge>> graph(n + 1, std::vector<Edge>());
  for (int i = 0; i < m; ++i) {
    int u, v, t_p, c;
    std::cin >> u >> v >> c >> t_p;
    graph[u].emplace_back(v, t_p, c);
    graph[v].emplace_back(u, t_p, c);
  }
  std::cout << earliest_time(graph, n, from, to) << std::endl;
  return 0;
}