#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)

using namespace std;
using LL = long long;
using P = pair<LL,int>;
const LL INF = (LL)1e18;

struct edge{
  int to;
  LL T, K;
  edge(int to, LL T, LL K) : to(to), T(T), K(K) {}
};

int main(){
  int N, M, X, Y;
  cin >> N >> M >> X >> Y;
  X--; Y--;
  vector<vector<edge>> G(N);
  rep(i,M){
    int a, b;
    LL t, k;
    cin >> a >> b >> t >> k;
    a--; b--;
    edge e1(b, t, k);
    edge e2(a, t, k);
    G[a].emplace_back(e1);
    G[b].emplace_back(e2);
  }
  priority_queue<P, vector<P>, greater<P>> q;
  vector<LL> dist(N, INF);
  dist[X] = 0;
  q.push(P(0, X));
  while(!q.empty()){
    P p = q.top(); q.pop();
    int u = p.second;
    LL d = p.first;
    if(d > dist[u]) continue;
    for(auto e : G[u]){
      int v = e.to;
      LL t = e.T, k = e.K;
      LL cost = t + k - d % k;
      if(d % k == 0) cost -= k;
      if(dist[v] < d + cost) continue;
      dist[v] = d + cost;
      q.push(P(dist[v], v));
    }
  }
  LL ans = dist[Y];
  if(ans == INF) ans = -1;
  cout << ans << endl;

  return 0;
}

#include<bits/stdc++.h>

using namespace std;

const int N=1e5+5;
const int mod=1e9+7;

bool visited[N];
long long dist[N];
vector<pair<int,pair<int,int>>>adj[N];
priority_queue<pair<long long,int>>pq;

void Dijkstra()
{
    int k,t,x,y;
    long long val;
    while(!pq.empty())
    {
        val=-pq.top().first;
        x=pq.top().second;
        pq.pop();
        if(visited[x])continue;
        visited[x]=1;
        for(auto it : adj[x])
        {
            y=it.first;
            t=it.second.first;
            k=it.second.second;
            if(dist[y] > ((val+k-1)/k)*k+t)
            {
                dist[y]=((val+k-1)/k)*k+t;
                pq.emplace(-dist[y],y);
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int a,b,i,k,m,n,t,x,y;
    cin >> n >> m >> a >> b;
    while(m--)
    {
        cin >> x >> y >> t >> k;
        adj[x].push_back({y,{t,k}});
        adj[y].push_back({x,{t,k}});
    }
    for(i=1;i<=n;i++)dist[i]=LLONG_MAX;
    dist[a]=0;
    pq.emplace(0,a);
    Dijkstra();
    if(dist[b] == LLONG_MAX)cout << "-1";
    else cout << dist[b];
    return 0;
}
//
