#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
#define SET0(a) memset(a,0,sizeof(a))
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define DWN(i,a,b) for(int i=(a);i>=(b);i--)

typedef long long ll;

const ll INF=LLONG_MAX;
typedef pair<ll,ll> PII;

const int N=1e5+5;
ll d[N];
struct node{
    ll to,next,w,k;
}e[N<<1];
int head[N],tot;
void add(ll u,ll v,ll w,ll k){e[tot].to=v;e[tot].w=w;e[tot].k=k;e[tot].next=head[u];head[u]=tot++;}
bool vis[N];
int n,m;
int s,t;

ll dijk(){
    for(int i=1;i<=n;i++) d[i]=INF;

    priority_queue<PII,vector<PII>,greater<PII> > pque;
    pque.push({0,s});
    d[s]=0;

    while(pque.size()){
        auto hd=pque.top(); pque.pop();
        int ver=hd.second;

        if(vis[ver]) continue;
        vis[ver]=true;

        for(int i=head[ver];~i;i=e[i].next){
            int go=e[i].to;
            int w=e[i].k-(d[ver]%e[i].k==0?e[i].k:d[ver]%e[i].k)+e[i].w;
            if(d[go]>d[ver]+w){
                d[go]=d[ver]+w;
                pque.push({d[go],go});
            }
        }
    }

    if(d[t]==INF) return -1;
    else return d[t];
}

int main(){
    memset(head,-1,sizeof head);
    cin>>n>>m>>s>>t;
    while(m--){
        ll u,v,w,k;
        cin>>u>>v>>w>>k;
        add(u,v,w,k);
        add(v,u,w,k);
    }

    cout<<dijk()<<endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vld = vector<ld>;
using vvld = vector<vld>;

#define fi first
#define se second
#define pb push_back
#define all(a) a.begin(),a.end()
#define rep(i,start,end) for(ll i=start;i<(ll)(end);i++)

using T = tuple<int,ll,ll>;
using P = pair<ll,int>;

int main(){
  int n,m,x,y;
  cin>>n>>m>>x>>y;
  x--;y--;

  vector<vector<T>> graph(n);
  rep(i,0,m){
    int a,b;
    ll t,k;
    cin>>a>>b>>t>>k;
    a--;b--;
    graph[a].pb({b,t,k});
    graph[b].pb({a,t,k});
  }

  vll dist(n,1e18);
  dist[x]=0;

  priority_queue<P,vector<P>,greater<P>> que;
  que.push({0,x});

  while(!que.empty()){
    auto [time,now] = que.top();
    que.pop();

    if(dist[now]<time){
      continue;
    }

    for(auto [next,time2,k]:graph[now]){
      ll start=time+(k-time%k)%k;
      if(dist[next]>start+time2){
        que.push({start+time2,next});
        dist[next]=start+time2;
      }
    }
  }

  if(dist[y]>=1e17){
    cout<<-1<<endl;
  }
  else{
    cout<<dist[y]<<endl;
  }
}