#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <queue>
using namespace std;
using ll = long long;
constexpr ll INF = 1LL << 60;
struct edge
{
    int to;
    ll a, b;
    edge(int to, ll a, ll b) : to(to), a(a), b(b) {}
};

ll calc(ll t, ll a, ll b)
{
    return t + (b - 1) / (a + t) + 1;
}

ll rt(const ll n)
{
    ll inf = 1, sup = 2 * std::sqrt(n);
    while (sup - inf > 1) {
        const ll mid = (sup + inf) / 2;
        if (mid * mid == n) { return mid; }
        (mid * mid > n ? sup : inf) = mid;
    }
    return inf;
}

ll ternary_seartch(ll t, ll a, ll b)
{
    //cout << "t,a,b = " << (ll)t << " " << a << " " << b << endl;
    // ll ok = t, ng = INF;
    // if ((ll)b <= a + t) return calc(t, a, b);
    // while (ng - ok > 2) {
    //     //cout << "ng, ok = " << (ll)ng << " " << (ll)ok << endl;
    //     ll d = (ng - ok) / 3, l = ok + d, r = ng - d;
    //     if (calc(l, a, b) <= calc(r, a, b))
    //         ng = r;
    //     else
    //         ok = l;
    // }
    // return min(min(calc(ok, a, b), calc(ng, a, b)), calc(ok + 1, a, b));
    const ll c = rt(b);
    if (c * c == b) {
        const ll o = c - a;
        if (o >= t) {
            return 2 * c - a;
        } else {
            return calc(t, a, b);
        }
    } else {
        if (t >= c - a + 1) {
            return calc(t, a, b);
        } else {
            return std::min(calc(c - a, a, b), calc(c - a + 1, a, b));
        }
    }
}

ll dijkstra(vector<vector<edge>>& G, int s, int t)
{
    int N = G.size();
    vector<ll> ret(N, INF);
    priority_queue<pair<ll, int>> wait;
    wait.emplace(0, s);
    while (!wait.empty()) {
        ll t = -wait.top().first;
        int v = wait.top().second;
        wait.pop();
        if (t > ret[v]) continue;
        ret.at(v) = t;
        for (size_t i = 0; i < G[v].size(); ++i) {
            edge e = G[v][i];
            ll a = e.a, b = e.b;
            int v_ = e.to;
            ll t_ = ternary_seartch(t, a, b);
            if (t_ < ret.at(v_)) {
                ret.at(v_) = t_;
                wait.emplace(-t_, v_);
            }
        }
    }
    return ret[t];
}

int main()
{
    int N, M, S, G;
    cin >> N >> M >> S >> G;
    --S;
    --G;
    vector<vector<edge>> H(N);
    for (int i = 0; i < M; ++i) {
        int u, v;
        ll a, b;
        cin >> u >> v >> a >> b;
        --u;
        --v;
        H[u].push_back(edge{v, a, b});
        H[v].push_back(edge{u, a, b});
    }
    ll ans = dijkstra(H, S, G);
    if (ans == INF)
        cout << -1 << endl;
    else
        cout << (ll)ans << endl;
    return 0;
}

