/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <fstream> 
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <iomanip> //setprecision
#include <map> // map
#include <unordered_map> //unordered_map
#include <queue> // queue, priority_queue
#include <set> // set,multiset
#include <stack> // stack
#include <deque> // deque
#include <math.h>//pow,,,
#include <cmath>//abs,,,
#include <bitset> // bitset
#include <numeric> //accumulate,,,
#include <sstream>
#include <random>
#include <initializer_list>
#define endl "\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
const long long MOD = 998244353;
//const long long MAXIMUM = INFINITY;

//Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M, long long mod) {
    if (M == 0) return 1;
    long long res = mod_pow((N * N) % mod, M / 2,mod);
    //When end-of-a bit is 1, times simple N.
    if (M & 1) res = (res * N) % mod;
    return res %= mod;
}
long long gcd(long long a, long long b) {
    if (b == 0) return a; else return gcd(b, a % b);
}
long long lcm(long long a, long long b) {
    return a / gcd(a, b) * b ;
}
long long get_mod(long long res){
    if(res < 0) res += MOD;
    return res % MOD;
}

typedef tuple<int,long long,long long> tup;

vector<vector<tup>> Edge(100005);
vector<long long> pre(100005,-1);
vector<long long> dist(100005,INF);

void dijkstra(){
    priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> PQ;
    PQ.emplace(0,0);
    dist.at(0) = 0;
    while(!PQ.empty()){
        int now = PQ.top().second;
        long long d = PQ.top().first;
        PQ.pop();
        if(dist.at(now) < d) continue;

        for(tup t : Edge.at(now)){
            int to = get<0>(t);
            long long C = get<1>(t);
            long long D = get<2>(t);
            long long r = d;
            // rが一番いい時間として、Di/t+1 + t+1 の最小値は√Diで取る（それはそう）
            if(sqrt(D) < d) r = d;
            else r = sqrt(D);
            long long COST = C + D/(r+1);
            if(dist[to] > COST + r){
                dist[to] = COST + r;
                PQ.emplace(COST+r , to);
            }
        }
    }   
}

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    /*FILE* in = freopen("answer.txt", "w", stdout);
    ifstream ifs("in.txt"); // 巨大ケースは in.txt に書いてifsで標準入力*/

    int N;cin >> N;
    int M;cin >> M;
    for(int i = 0;i<M;i++){
        int a,b;long long c,d;cin >> a >> b >> c >> d;a--;b--;
        if(a == b) continue;
        Edge.at(a).emplace_back(b,c,d);
        Edge.at(b).emplace_back(a,c,d);
    }

    dijkstra();

    if(dist[N-1] == INF){
        cout << -1 << endl;
    }
    else{
        cout << dist[N-1] << endl;
    }
    
}
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip>
#include <limits>
#include <math.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(a) a.begin(),a.end()
#define P pair<int,int>
#define Pll pair<long,long>
#define mp make_pair
#define INF 1000000001
#define INFl 1000000000000000001
#define ll long long
using namespace std;



template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

long time(long t,long c,long d){
    if(t<0)return INFl;
    return c+d/(t+1);
}

struct edge{long to,c,d;};
 
int main() {
    int N,M;cin>>N>>M;


    int X,Y,V;//始点X終点Y頂点数V
    V=N;
    vector< vector< edge > > G(V,vector<edge>(0));
    rep(i,M){
        long a,b,c,d;cin>>a>>b>>c>>d;
        if(a==b)continue;
        a--;b--;
        edge e={b,c,d};
        G[a].push_back(e);
        edge e2={a,c,d};
        G[b].push_back(e2);
    }
    X=0;Y=N-1;

    vector<long>d(V,INFl);
    priority_queue<Pll,vector<Pll>,greater<Pll> > que;
    for(int i=0;i<V;i++){d[i]=INFl;}
    d[X]=0;
    que.push(Pll(0,X));//コスト，頂点
 
    while(!que.empty()){
        Pll p=que.top();que.pop();
        long v=p.second;
        if(d[v]<p.first)continue;
        for(int i=0;i<G[v].size();i++){
            edge e=G[v][i];
            long to=e.to;
            long c=e.c;
            long t=d[v];
            long D=e.d;
            long left=t;
            long right=INFl/10;

            for(int i=0;i<100;i++){
                long c1=(left*2+right)/3;
                long c2=(left+right*2)/3;
                long a1=c1+time(c1,c,D);
                long a2=c2+time(c2,c,D);
                if(a1>=a2){
                    left=c1;
                }else{
                    right=c2;
                }
            }
            long cost=INFl;
            long pc=INFl;
            for(long x=250;x>=-250;--x){
                if(left+x<t)break;
                cost=min(cost,left+x+time(left+x,c,D));
                if(cost>pc)break;
                pc=cost;
            }
            //cout<<v<<" "<<to<<" "<<left<<" "<<cost<<endl;
            if(d[to]>cost){
                d[to]=cost;
                que.push(Pll(d[to],to));
            }
        }
    }
    if(d[Y]==INFl)cout<<-1<<endl;
    else cout<<d[Y]<<endl;
}