#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define faster ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define read freopen("in.txt", "r", stdin)
#define write freopen("out.txt", "w", stdout)
#define var(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define mem(x, n) memset(x, n, sizeof(x))
#define all(x) x.begin(), x.end()
#define endl "\n"

const int N = 1e5 + 5;

struct station
{
	ll city, time, k;
};

vector<station> g[N];
ll dist[N];

int main()
{
	faster;

	mem(dist, -1);

	ll n, m, x, y;
	cin >> n >> m >> x >> y;

	for (int i = 0; i < m; i++)
	{
		int a, b, t, k;
		cin >> a >> b >> t >> k;
		station curr, curr1;
		curr.city = b, curr.time = t, curr.k = k;
		curr1 = curr;
		curr1.city = a;
		g[a].push_back(curr);
		g[b].push_back(curr1);
	}

	priority_queue<pll,  vector<pll>,greater<pll>> pq;
	dist[x] = 0;
	// first = from, second = time
	pq.emplace(x, 0);

	while (pq.size())
	{
		auto fr = pq.top();
		pq.pop();
		if (dist[fr.first] != -1 && dist[fr.first] < fr.second)
		{
			continue;
		}

		for (auto next : g[fr.first])
		{
			ll nextStartTime = ((fr.second + next.k - 1) / next.k) * next.k;
			if (dist[next.city] == -1 || nextStartTime + next.time < dist[next.city])
			{
				dist[next.city] = nextStartTime + next.time;
				pq.emplace(next.city, dist[next.city]);
			}
		}
	}

	cout << dist[y] << endl;

	return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define mp(a,b) make_pair(a,b)
#define vi vector<int>
#define mii map<int,int>
#define mpi map<pair<int,int>,int>
#define vp vector<pair<int,int> >
#define pb(a) push_back(a)
#define fr(i,n) for(i=0;i<n;i++)
#define rep(i,a,n) for(i=a;i<n;i++)
#define F first
#define S second
#define endl "\n"
#define fast std::ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define dom 998244353
#define pi 3.14159265
#define sl(a) (int)a.length()
#define sz(a) (int)a.size()
#define all(a) a.begin(),a.end()
#define pii pair<int,int> 
#define mini 2000000000000000000
#define time_taken 1.0 * clock() / CLOCKS_PER_SEC
//mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
//primes for hashing 937, 1013
template<typename T, typename U> static inline void amin(T &x, U y) 
{ 
    if (y < x) 
        x = y; 
}
template<typename T, typename U> static inline void amax(T &x, U y) 
{ 
    if (x < y) 
        x = y; 
}
void shikhar7s(int cas)
{
    int n,m,x,y;
    cin>>n>>m>>x>>y;
    vector<pair<pii,pii> > adj[n+5];
    int u,v,w[m],z,i;
    fr(i,m)
    {
        cin>>u>>v>>w[i]>>z;
        adj[u].pb(mp(mp(v,i+1),mp(w[i],z)));
        adj[v].pb(mp(mp(u,i+1),mp(w[i],z)));
    }
    mpi d;
    d[mp(x,0ll)]=0;
    set<pair<int,pii> > s;
    s.insert(mp(0ll,mp(x,0ll)));
    int ans=mini;
    while(sz(s))
    {
        auto it=s.begin();
        int dis=(*it).F;
        u=(*it).S.F;
        v=(*it).S.S;
        s.erase(it);
        if(u==y&&!v)
        {
            cout<<dis;
            return;
        }
        if(v>0)
        {
            if(!d.count(mp(u,0ll))||d[mp(u,0ll)]>dis+w[v-1])
            {
                if(s.find(mp(d[mp(u,0ll)],mp(u,0ll)))!=s.end())
                    s.erase(mp(d[mp(u,0ll)],mp(u,0ll)));
                d[mp(u,0ll)]=dis+w[v-1];
                s.insert(mp(d[mp(u,0ll)],mp(u,0ll)));
            }
        }
        else
        {
            fr(i,sz(adj[u]))
            {
                v=adj[u][i].F.F;
                z=adj[u][i].S.S;
                int j=adj[u][i].F.S;
                if(!d.count(mp(v,j))||d[mp(v,j)]>dis+((z-(dis%z))%z))
                {
                    if(s.find(mp(d[mp(v,j)],mp(v,j)))!=s.end())
                        s.erase(mp(d[mp(v,j)],mp(v,j)));
                    d[mp(v,j)]=dis+((z-(dis%z))%z);
                    s.insert(mp(d[mp(v,j)],mp(v,j)));
                }
            }
        }
    }
    cout<<-1;
}
signed main()
{
    fast;
    //freopen("input.txt", "rt", stdin);
    //freopen("output.txt", "wt", stdout);
    int t=1;
    //cin>>t;
    int cas=1;
    while(cas<=t)
    {
    shikhar7s(cas);
    cas++;
    }
    return 0;
}