#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, int> P;
struct edge{
    int to;
    long long cost;
    long long time;
};
int main(){
    int N, M, X , Y;
    cin >> N >> M >> X >> Y;
    X--;
    Y--;
    vector<vector<edge>> G(N);
    for(int i = 0; i < M; i++){
        int A, B;
        cin >> A >> B;
        A--;
        B--;
        long long T, K;
        cin >> T >> K;
        G[A].push_back({B, T, K});
        G[B].push_back({A, T, K});
    }
    long long INF = 1e18;
    priority_queue<P, vector<P>, greater<P>> q;
    vector<long long> d(N, INF);
    d[X] = 0;
    q.push(P(0, X));
    while(!q.empty()){
        P v = q.top();
        q.pop();
        if(d[v.second] < v.first) continue;
        for(edge e: G[v.second]){
            long long t = (v.first + e.time - 1) / e.time * e.time;
            if(d[e.to] > t + e.cost){
                d[e.to] = t + e.cost;
                q.push(P(d[e.to], e.to));
            }
        }
    }
    if(d[Y] == INF) cout << -1 << endl;
    else cout << d[Y] << endl;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
int main() {
	
	ll n,m,x,y;
	cin>>n>>m>>x>>y;
	vector<ll> adj[n+1],dis[n+1],tim[n+1],ans(n+1);
	for(ll i=0;i<m;i++){
		ll a,b,c,d;
		cin>>a>>b>>c>>d;
		adj[a].push_back(b);
		adj[b].push_back(a);
		dis[a].push_back(c);
		dis[b].push_back(c);
		tim[a].push_back(d);
		tim[b].push_back(d);
	}
	for(ll i=1;i<n+1;i++)
	ans[i]=-1;
	ans[x]=0;
	queue<ll> q;
	q.push(x);
	while(!q.empty()){
		ll x1=q.front();
		q.pop();
		for(ll i=0;i<adj[x1].size();i++){
			ll x2=ans[x1];
			if(x2%tim[x1][i]!=0){
				x2+=tim[x1][i]-(x2%tim[x1][i]);
			}
			if((ans[adj[x1][i]]==-1)||(x2+dis[x1][i]<ans[adj[x1][i]])){
				ans[adj[x1][i]]=x2+dis[x1][i];
				q.push(adj[x1][i]);
			}
		}
	}
	cout<<ans[y];
	return 0;
}