#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <string>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <ctime>
#include <cassert>
#include <fstream>
#include <stack>
#include <random>
#include <iomanip>

#include <fstream>


using namespace std;
typedef long long ll;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
ll inf=(ll)1E18;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
dd EPS=1E-9;
#define endl "\n"
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define N 100000
struct edge{ll to,cost,k; };
vector<edge> G[N+1];
ll d[N+1];


void dijkstra(ll s){
    fill(d,d+(N+1),inf);
    d[s]=0;
    priority_queue<l_l,vector<l_l>,greater<l_l>> que;
    que.push(l_l(0,s));
    while(!que.empty()){
        l_l p=que.top();que.pop();
        ll v=p.second;
        if(d[v]<p.first)continue;
        if(G[v].size()>0){
            rep(i,0,G[v].size()-1){
                edge e=G[v][i];
                ll k=e.k;
                ll time= k*((d[v]+k-1)/k) + e.cost;
                if(d[e.to]>time){
                    d[e.to]=time;
                    que.push(l_l(d[e.to],e.to));
                }
            }
        }
    }
}


int main(){fastio
    ll n,m,x,y;cin>>n>>m>>x>>y;
    ll a[m],b[m],t[m],k[m];
    rep(i,0,m-1)cin>>a[i]>>b[i]>>t[i]>>k[i];
    rep(i,0,m-1){
        edge e;
        e.to=b[i];
        e.cost=t[i];
        e.k=k[i];
        G[a[i]].pb(e);
        e.to=a[i];
        e.cost=t[i];
        e.k=k[i];
        G[b[i]].pb(e);
    }
    
    dijkstra(x);
    if(d[y]>=inf){
        cout<<-1<<endl;
    }else{
        cout<<d[y]<<endl;
    }
    
    
    return 0;
    
}






/*#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")*/
// only when really needed

/* GNU G++17 7.3.0: No long long for faster code
   GNU G++17 9.2.0 (64 bit, msys 2): Long long only for faster code */
 
#include <bits/stdc++.h>
  
#define for1(i,a,b) for (int i = a; i <= b; i++)
#define for2(i,a,b) for (int i = a; i >= b; i--)
#define int long long
 
#define sz(a) (int)a.size()
#define pii pair<int,int>
 
/*
__builtin_popcountll(x) : Number of 1-bit
__builtin_ctzll(x) : Number of trailing 0
*/
 
#define PI 3.1415926535897932384626433832795
#define INF 1000000000
#define MOD 1000000007
#define MOD2 1000000009
#define EPS 1e-6
 
using namespace std;

signed main() {
    
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    
    int n, ans = INF;
    cin >> n;
    
    for1(i,1,n) {
        
        int a, p, x;
        cin >> a >> p >> x;
        if (a < x) ans = min(ans, p);
        
    }
    
    if (ans == INF) cout << -1;
    else cout << ans;
    
}