#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

struct Edge{
  int to;
  ll T;
  ll K;

  Edge(){}
  Edge(int to,ll T,ll K):to(to),T(T),K(K){}
};

const ll INF=1e18;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N,M,X,Y;
  cin>>N>>M>>X>>Y;
  X--;Y--;
  vector<vector<Edge>> g(N);
  rep(i,M){
    int a,b,t,k;
    cin>>a>>b>>t>>k;
    a--;b--;
    g[a].push_back(Edge{b,t,k});
    g[b].push_back(Edge{a,t,k});
  }

  vector<ll> dist(N,INF);
  dist[X]=0;
  priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>> PQ;
  PQ.push(mkp(0,X));
  while(!PQ.empty()){
    auto f=PQ.top();
    PQ.pop();

    int now=f.second;
    ll cost=f.first;
    if(dist[now]<cost) continue;

    for(auto &e:g[now]){
      ll neco=cost+(e.K-cost%e.K)%e.K+e.T;
      if(dist[e.to]>neco){
        dist[e.to]=neco;
        PQ.push(mkp(neco,e.to));
      }
    }
  }

  if(dist[Y]==INF) cout<<-1<<endl;
  else cout<<dist[Y]<<endl;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;

int a, b, c, d;

int main(){
    cin>>a>>b>>c>>d;
    //max x-y 
    cout<<max(a,b) - min(c,d)<<"\n";
}