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

const ll inf = 1e18;
struct Edge { int to, t, 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, 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});
  }

  priority_queue<pll, vector<pll>, greater<pll>> q;
  q.push(pll{0, x});

  vector<ll> d(n, 1e18);
  d[x] = 0;
  while (!q.empty()) {
    pll p = q.top();
    q.pop();
    if (d[p.second] < p.first) continue;
    for (auto a : g[p.second]) {
      ll td = p.first;
      td += (a.k - p.first % a.k) % a.k;
      td += a.t;
      if (td < d[a.to]) {
        d[a.to] = td;
        q.push(pll{td, a.to});
      }
    }
  }

  ll ans = d[y];
  if (ans == inf) ans = -1;
  cout << ans << endl;
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define N 301000
#define M 601000
#define inf 1E16
typedef long long ll;

struct node{
	int v, next; ll w, kk;
}e1[M];
int head1[N];
ll dist1[N], inq1[N];
int n, m, x, y;
ll t[N], k[N];
int len = 0;

void add_edge1(int x,int y,ll w,ll kk){
	e1[++len].v=y;
	e1[len].w=w;
	e1[len].next=head1[x];
    e1[len].kk = kk;
	head1[x]=len;
	return ;
}

ll calc(ll a, ll b){
    if(a % b == 0) return a;
    else return (a / b + 1ll) * b;
}
void spfa1(){
    // cout<<inf<<endl;
	int i,j,k,u,v;
    ll w;
	queue<int> q;
	for(i = 1; i <= n; i++){
		dist1[i] = inf; inq1[i] = 0;
	}
	q.push(x);
	dist1[x] = 0;
	inq1[x] = 1;
	while(!q.empty()){
		u = q.front();
		q.pop();
		inq1[u] = 0;
		for(i = head1[u]; i != -1; i = e1[i].next){
			v = e1[i].v; w = e1[i].w;
            ll tmp = calc(dist1[u], e1[i].kk);
			if(dist1[v] > tmp + w){
				dist1[v] = tmp + w;
				if(!inq1[v]){
					q.push(v);
					inq1[v] = 1;
				}
			}
		}
	}
}

void init(){
	int i,u,v,w;
	scanf("%d%d%d%d", &n, &m, &x, &y);
	memset(head1, -1, sizeof(head1));
	for(i = 1; i <= m; i++){
		scanf("%d%d%lld%lld", &u, &v, &t[i], &k[i]);
		add_edge1(u, v, t[i], k[i]);
        add_edge1(v, u, t[i], k[i]);
	}
	return ;
}
void solve(){
	int i,j,k;
	spfa1();
	ll ans = dist1[y];
    if(ans != inf) printf("%lld\n", ans);
    else cout<<-1<<endl;
    return ;
}

int main(){
	init();
	solve();
	return 0;
}
