#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <iterator>
#include <math.h>
#include <bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define mp make_pair
#define MOD 1000000007;
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    // int T,p;
    // cin>>T; 
    // for(p=1;p<=T;p++)
    // {
            ll N,M,X,Y;
            cin>>N>>M>>X>>Y;
            ll a,b,c,i,j,k;
            ll A[M][4];
            vector <vector <ll> > vect(N+1);
            ll dp[N+1];
            
            memset(dp,-1,sizeof(dp));
            
            queue <ll> q;

            for(i=0;i<M;i++)
            {
                    cin>>A[i][0]>>A[i][1]>>A[i][2]>>A[i][3];
                    vect[A[i][0]].pb(i);
                    vect[A[i][1]].pb(i);
            }

            q.push(X);
            dp[X]=0;
            bool x[N+1];
            memset(x,false,sizeof(x));
            x[X]=true;
            while(q.size()!=0)
            {
                a=q.front();
                q.pop();
                if(dp[a]==-1)
                b=1e15;
                else
                b=dp[a];
                for(i=0;i<vect[a].size();i++)
                {
                    if(A[vect[a][i]][0]==a)
                    c=A[vect[a][i]][1];
                    else
                    c=A[vect[a][i]][0];
                    // cout<<a<<" "<<c<<" cesq\n";
                    if(x[c]==false)
                    {
                        q.push(c);
                        // cout<<c<<" chek2112\n";
                        x[c]=true;
                    }
                    if(dp[c]==-1)
                    {
                        continue;
                    }
                    else
                    {
                        if(dp[c]%A[vect[a][i]][3]==0)
                        b=min(dp[c]+A[vect[a][i]][2],b);
                        else
                        {
                            b=min(b,A[vect[a][i]][2]+dp[c]+A[vect[a][i]][3]-dp[c]%A[vect[a][i]][3]);
                        }
                    }
                }
                // cout<<b<<" check12\n";
                dp[a]=b;

                for(i=0;i<vect[a].size();i++)
                {
                    if(A[vect[a][i]][0]==a)
                    c=A[vect[a][i]][1];
                    else
                    c=A[vect[a][i]][0];
                    

                    if(dp[a]%A[vect[a][i]][3]==0)
                    b=dp[a]+A[vect[a][i]][2];
                    else
                    {
                         b=A[vect[a][i]][2]+dp[a]+A[vect[a][i]][3]-dp[a]%A[vect[a][i]][3];
                    }
                    // cout<<b<<" "<<a<<" \n";
                    if(dp[c]==-1)
                    {
                        dp[c]=b;
                    }
                    else
                    {
                        dp[c]=min(dp[c],b);
                    }
                }
            }
            cout<<dp[Y];
            return 0;
}
#include <bits/stdc++.h>
using namespace std;
 
#define pb push_back
#define S second
#define F first
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define vll vector<long long int>
#define pll pair<long long int,long long int>
/* unordered_map<int,int>mp; mp.reserve(1024); mp.max_load_factor(0.25); */
#define mod 1000000007
#define mod2 998244353
#define ll long long int
#define ld long double
#define pi 3.141592653589793238
#define Endl endl
#define endl "\n"
const int N = 1e5 + 5;
const ll inf = 1e18;
vector<vll> a[N];
void solve()
{   
        ll n , m , x , y;
        cin >> n >> m >> x >> y;
        if(m == 0)
        {
                cout << -1;
                return;
        }
        for(ll i=1;i<=m;i++)
        {
                ll u , v , t , k;
                cin >> u >> v >> t >> k;
                a[u].pb({v,t,k});
                a[v].pb({u,t,k});
        }
        priority_queue<pll,vector<pll>,greater<pll>> pq;
        vector<ll> vis(n+1,1e18);
        pq.push({x,0});
        vis[x] = 0;
        ll ans = 1e18;
        while(!pq.empty())
        {
                pll p = pq.top();
                pq.pop();
                ll u = p.F;
                ll time = p.S;
                if(u == y)
                        ans = min(ans,time);
                for(auto v:a[u])
                {
                        ll tt = 0;
                        if(time%v[2] == 0)
                                tt = time + v[1];
                        else
                                tt = (time/v[2] + 1)*v[2] + v[1];
                        if(vis[v[0]] > tt)
                        {
                                pq.push({v[0],tt});
                                vis[v[0]] = tt;
                        }
                }
        }
        if(ans == 1e18)
                cout << -1;
        else
                cout << ans;
}

void debug(ll tt) {}
 
signed main()
{
        FAST;
        int t = 1;
        // cin >> t;
        while(t--)
        {
                solve();
        }
}