#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
using P=pair<int,int>;
using ll=long long;
using ld=long double;

int main(){
  ll n,m,x,y,inf=1e18;
  cin >> n >> m >> x >> y;
  x--; y--;
  vector<vector<vector<ll>>> a(n,vector<vector<ll>>(0,vector<ll>(3)));
  rep(i,m){
    ll c,d,t,k;
    cin >> c >> d >> t >> k;
    c--; d--;
    a[c].emplace_back();
    a[d].emplace_back();
    a[c][(int)a[c].size()-1]={d,t,k};
    a[d][(int)a[d].size()-1]={c,t,k};
  }
  
  vector<ll> b(n,inf);
  queue<int> q;
  q.push(x); b[x]=0;
  while(q.size()){
    int now=q.front(); q.pop();
    rep(i,(int)a[now].size()){
      ll p=(b[now]-1)/a[now][i][2]+1;//発車時刻
      p*=a[now][i][2];
      if(b[now]==0) p=0;
      if(b[a[now][i][0]]>p+a[now][i][1]){
        b[a[now][i][0]]=p+a[now][i][1];
        q.push(a[now][i][0]);
      }
    }
  }
  if(b[y]==inf) cout << -1 << endl;
  else cout << b[y] << endl;
}
#pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")
 
#include <bits/stdc++.h>
 
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#define pb push_back
#define fi first
#define se second
#define ins insert
#define sz(x) (int)x.size()
#define dbg(x) cout << x << "\n";
 
const int N = 1e5 + 5;
const long long int mod = 1e9 + 7;
const long long int Mod = 998244353;
const long double Pi = acos(-1);
const long long int Inf = 4e18;
 
using namespace std;

struct nxt{
        long long int to, ti, in;
};

vector <struct nxt> g[N];
vector <long long int> ans(N);

long long int Dijkstra (long long int x, long long int y){
        ans.assign(N, Inf);
        ans[x] = 0;
        set < pair <long long int, long long int> > s;
        s.ins({0, x});
        while(!s.empty()){
                long long int cur = s.begin()->fi, idx = s.begin()->se;
                s.erase(s.begin());
                for(auto u: g[idx]){
                        long long int upd = cur;
                        if(cur % u.in != 0){
                                upd = ((cur + u.in - 1)/u.in) * u.in;
                        }
                        if(ans[u.to] > upd + u.ti){
                                ans[u.to] = upd + u.ti;
                                s.ins({ans[u.to], u.to});
                        }
                } 
        }
        if(ans[y] == Inf) return -1;
        else return ans[y];
}
 
void TestCase (){
        long long int n, m, x, y;
        cin >> n >> m >> x >> y;
        for(int i = 1; i<=m; i++){
                long long int u, v, t, k;
                cin >> u >> v >> t >> k;
                g[u].pb({v, t, k});
                g[v].pb({u, t, k});
        }
        cout << Dijkstra(x, y);
}
 
int main(){
        IOS;
        int T = 1;
        // cin >> T;
        while(T--){
                TestCase();
        }
        return 0;
}