//#include <atcoder/maxflow.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
 

#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <bitset>
#include <fstream>
 
 
#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

 
//using namespace __gnu_pbds;
//using namespace atcoder;
using namespace std;

//const ll modd = (1000LL * 1000LL * 1000LL + 7LL);
const ll modd = 998244353;


int main(int argc, char *argv[])   {

    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no

    ios_base::sync_with_stdio(false);
    cin.tie(0);
 
    cout.precision(12);

//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(n); readint(m);
        struct info {
            int i;
            ll c,d;
        };
        vector<vector<info>> adj(n);
        forr(i,0,m) {
            readint(aa); --aa; readint(bb); --bb;
            readll(c); readll(d);
            if (aa!=bb) {
              adj[aa].push_back({bb,c,d});
              adj[bb].push_back({aa,c,d});
            }
        }
        vector<bool> visited(n, false);
        stack<int> todo; todo.push(0);
        while (!todo.empty()) {
            auto curr = todo.top(); todo.pop();
            if (visited[curr]) { continue; }
            visited[curr] = true;
            for(auto x : adj[curr]) {
                todo.push(x.i);
            }
        }
        if (!visited.back()) {
            cout << -1 << endl;
            continue;
        }

        priority_queue<pair<ll,int>> todoo; todoo.push(make_pair(0,0));
        vector<bool> visit(n, false);
        while (!todoo.empty()) {
            auto curr = todoo.top(); todoo.pop();
            if (visit[curr.second]) { continue; }
            visit[curr.second] = true;
            ll tim = -curr.first;
            if (curr.second==n-1) {
                cout << tim << endl;
                return 0;
            }
            for(auto x : adj[curr.second]) {
              vector<ll> candidates; candidates.push_back(0);
              ll p = (ll)sqrt(x.d) - 1-tim;
              candidates.push_back(p-2);
              candidates.push_back(p-1);
              candidates.push_back(p);
              candidates.push_back(p+1);
              candidates.push_back(p+2);
              for(ll w : candidates) {
                if (w>=0)  todoo.push(make_pair(-(tim + x.c + (x.d/(tim+1+w)) + w ), x.i));
              }

            }
        }

    }

    return 0;
}

#include <iostream>
#include <vector>
#include  <numeric>
#include<cstdlib>
#include <map>
#include <set>
#include <math.h>
#define ll long long
#define MAXN 100000
using namespace std;


std::vector<std::tuple<int,ll,ll>> edges[MAXN+2];
ll GetOptTime(ll d) {
    // t + d/(t+1) is minimized
    ll topt = (ll)sqrt(d);
    ll opt_time = -1;
    for(ll t=max(0ll,topt-20);t<=topt+20;t++) {
        ll curr_time = t+d/(t+1);
        if (opt_time == -1 || curr_time < opt_time) {
            topt = t;
            opt_time = curr_time;
        }
    }
    return topt;
}

bool done[MAXN+2];

int main() {
    int N,M;
    cin>>N>>M;
    for(int i=1;i<=M;i++) {
        int a,b;
        ll c,d;
        cin>>a>>b>>c>>d;
        edges[a].push_back(std::make_tuple(b,c,d));
        edges[b].push_back(std::make_tuple(a,c,d));
    }

    for(int u=1;u<=N;u++) {
        done[u]=false;
    }

    set<pair<ll,int>> Q;
    Q.insert({0,1});
    while(!Q.empty()) {
        auto it = Q.begin();
        int u = it->second;
        ll curr_time = it->first;
        Q.erase(it);
        if (done[u]) continue;
        if (u == N) {
            cout << curr_time << endl;
            return 0;
        }
        done[u]=true;
        for(auto vcd : edges[u]) {
            int v = std::get<0>(vcd);
            ll c = std::get<1>(vcd);
            ll d = std::get<2>(vcd);
            // t + c + d/(t+1) for t >= curr_time
            ll opt_time = max(curr_time, GetOptTime(d));
            Q.insert({opt_time+c+d/(opt_time+1),v});
        }
    }

    cout << -1 << endl;


}