// E - Rush Hour 2
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
#define vec vector
using vl = vec<ll>;
#define rep(i,e) for(int i=0;i<(e);++i)
using PR = pair<ll,int>;

// 本問用に修正
// dijkstra O(|E|log|V|)
int V;
vec<tuple<int,int,int,int>> E;
vec<vec<int>> G;
ll INF = 1e18;

vl dijkstra(int s){
	vl dist(V, INF);
	dist[s] = 0;
	priority_queue<PR, vec<PR>, greater<PR>> pq;
	pq.push({0, s});
	while(pq.size()){
		auto[t, v] = pq.top(); pq.pop();
		if(dist[v] < t) continue;
		for(int&i:G[v]){
			auto&[a, b, c, d] = E[i];
			int u = a==v? b:a;
			ll k = max(t, ll(sqrt(d) - .5));
			ll nt = k + c + d / (k+1);
			if(dist[u] <= nt) continue;
			dist[u] = nt;
			pq.push({nt, u});
		}
	}
	return dist;
}

int main(){
	int m; cin>>V>>m;
	G.resize(V);
	rep(i, m){
		int a, b, c, d; cin>>a>>b>>c>>d; --a, --b;
		E.emplace_back(a, b, c, d);
		G[a].push_back(i), G[b].push_back(i);
	}
	ll ans = dijkstra(0)[V-1];
	if(ans == INF) ans = -1;
	cout<< ans <<endl;
}

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

#define int long long
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define ff first
#define ss second
// #define endl '\n'
#define mod 1000000007

int bin(int x, int n){
    int res=1; x%=mod;
    while(n){
        if(n&1) res=(res*x)%mod;
        x=(x*x)%mod;
        n>>=1;
    }
    return res;
}

const int MXN = 2e5 + 1;
vector<pair<int, pair<int, int> > > adj[MXN];
int n, m;
int a[MXN];

int fun(int x, int t){
    if(t > x) return t;
    int l = sqrt(x) - 3;
    int r = sqrt(x) + 3;
    int mx = 1e18, ans = 0;
    for(int i = l; i <= r; i++){
        if(i < 0 || i > x) continue;
        if(i + x/(i + 1) < mx){
            mx = i + x/(i + 1);
            ans = i;
        }
    }
    return ans;
}

void solve(){
    cin >> n >> m;
    for(int i = 1; i <= m; i++){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        adj[a].pb({b, {c, d}});
        adj[b].pb({a, {c, d}});
    }
    priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
    int time[n + 1], vis[n + 1] = {0};
    for(int i = 2; i <= n; i++) time[i] = 1e18;
    time[1] = 0;
    pq.push({0, 1});
    while(pq.size()){
        auto p = pq.top();
        pq.pop();
        int u = p.ss;
        if(vis[u]) continue;
        vis[u] = 1;
        if(u == n) return void(cout << time[n] << endl);
        // cout << u << endl;
        for(auto i : adj[u]){
            if(vis[i.ff]) continue;
            int t = time[u];
            int tmp, x = sqrt(i.ss.ss);
            tmp = t + i.ss.ss/(t + 1);
            if(t < x){
                tmp = min(tmp, x + i.ss.ss/(x + 1));
            }
            if(t < x - 1 && x > 0){
                tmp = min(tmp, x - 1 + i.ss.ss/(x));
            }
            // cout << t << endl;
            if(time[i.ff] > tmp + i.ss.ff){
                time[i.ff] = tmp + i.ss.ff;
                pq.push({time[i.ff], i.ff});
            }
        }
    }
    // for(int i = 1; i <= n; i++) cout << time[i] << ' ';
    if(time[n] == 1e18) cout << -1 << endl;
    else cout << time[n] << endl;
}


signed main(){
    fast
    int t = 1;
    // cin >> t;
    for(int i = 1; i <= t; i++){
        solve();
    }
}
