#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>
#include <cassert>
#include <random>
#include <chrono>

/* #include <atcoder/all> */
/* using namespace atcoder; */
using namespace std;

void debug_out(){ cout << "\n"; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cout << H << " ";
  debug_out(T...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll MOD = 1000000007;
/* const ll MOD = 998244353; */
const ll INF = 1ll<<60;
const double PI = acos(-1);

struct INIT { INIT(){
  cin.tie(0); ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}}INIT;

struct Edge {
  int to, t, k;
  Edge(int to, int t, int k): to(to), t(t), k(k) {}
};

signed main() {
  int N, M, s, g;
  cin >> N >> M >> s >> g;
  s--; g--;

  vector<vector<Edge>> graph(N);
  REP(i, M){
    int u, v, t, k;
    cin >> u >> v >> t >> k;
    u--; v--;
    graph[u].emplace_back(Edge(v, t, k));
    graph[v].emplace_back(Edge(u, t, k));
  }

  vector<int> dist(N, INF);
  dist[s] = 0;

  using P = pair<int, int>;
  priority_queue<P, vector<P>, greater<P>> q;
  q.emplace(0, s);

  while(!q.empty()){
    auto [d, v] = q.top(); q.pop();
    if(dist[v] < d) continue;

    for(auto u : graph[v]){
      int nd = (d+u.k-1)/u.k*u.k + u.t;
      if(chmin(dist[u.to], nd)){
        q.emplace(nd, u.to);
      }
    }
  }
  
  if(dist[g] == INF) dist[g] = -1;
  cout << dist[g] << "\n";

  return 0;
}

#include <bits/stdc++.h>
#define INF 10000000000000
using namespace std;
typedef long long ll;
const int maxn=3e5+5;
ll n,m,s,t;
struct op{
    ll v;
    ll w,k;
    ll next;
}e[maxn*2];
ll head[maxn*2],cnt;
void add(int u,int v,ll w,ll k){
    e[++cnt]={v,w,k,head[u]};
    head[u]=cnt;
}
ll dis[maxn];
ll vis[maxn];
struct node{
    ll w;
    ll now;
    bool operator <(const node &x)const{
        return w>x.w;
    }
};
priority_queue<node>q;
void dijkstra(){
    memset(dis,127, sizeof(dis));
    dis[s]=0;
    q.push((node){0,s});
    while(!q.empty()){
        node x=q.top();
        q.pop();
        ll u=x.now;
        if(vis[u])continue;
        vis[u]=1;
        for (int i = head[u]; i ; i=e[i].next) {
            ll v=e[i].v;
            if((dis[v]>dis[u]+e[i].w+e[i].k-dis[u]%e[i].k&&dis[u]%e[i].k!=0)||(dis[v]>dis[u]+e[i].w&&dis[u]%e[i].k==0))
            {
                if(dis[u]%e[i].k!=0)
                    dis[v]=dis[u]+e[i].w+e[i].k-dis[u]%e[i].k;
                else dis[v]=dis[u]+e[i].w;
                q.push({dis[v],v});
            }
        }
    }
}
int main()
{
    scanf("%lld %lld %lld %lld",&n,&m,&s,&t);
    ll a,b;
    ll c,d;
    for (int i = 1; i <= m; ++i) {
        scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
        add(a,b,c,d);
        add(b,a,c,d);
    }
    dijkstra();
    printf("%lld\n",!vis[t]?-1:dis[t]);
    return 0;
}
