#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <regex>
#include <complex>
using namespace std;
#define all(v) v.begin(),v.end()
#define GS std::greater<int>()
#define FOR(i,l,r) for(Int i=(l);i<(r);i++)
#define DCOUT cout<<fixed<<setprecision(15) //DCOUT<<d<<endl;
#define ROUNDUP(a,b) ((a)+((b)-1))/(b)
#define TDVEC(name,type,h,w) vector<vector<type>>name((h),vector<type>(w))
#define P pair<Int,Int>
#define MOD 1000000007
#define MOD2 998244353
#define INF (1<<29)
#define LINF (1LL<<60)
#define PI 3.1415926535897932384626433832795028
#define itn int
#define space ' '
typedef long long Int;
typedef unsigned long long uInt;
typedef long double ld;
Int gcd(Int a,Int b){return b?gcd(b,a%b):a;}
Int lcm(Int a,Int b){return a/gcd(a,b)*b;}

int main(){
	Int a,b,c,d;cin>>a>>b>>c>>d;
	cout<<a*d-b*c<<endl;
	return 0;
}
#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
#define rep(i,n) for(ll i=0; i<n; i++)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rrep(i,n) for(ll i=n-1; i>=0; i--)
#define fi first
#define se second
#define pcnt __builtin_popcountll
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;
typedef pair<ll,Pll> PlP;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<class A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}cout << "\n";}
template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}
string zero_padding(int val, int nf){ ostringstream sout;sout << setfill('0') << setw(nf) << val; return sout.str();};
const ld eps = 1e-10;
ull mo = 1000000007;
ld PI=asin(1)*2;
//using namespace atcoder;

vector<vector<PlP>> G(100010);
int main(){ 
    ll N,M;
    cin >> N >> M;
    vector<ll> A(M), B(M), C(M), D(M);
    rep(i,M){
        cin >> A[i] >> B[i] >> C[i] >> D[i];
        A[i]--; B[i]--;
    }
    rep(i,M){
        G[A[i]].push_back({C[i],{D[i],B[i]}});
        G[B[i]].push_back({C[i],{D[i],A[i]}});
    }
    vector<ll> d1(N,1e18);
    priority_queue<Pll, vector<Pll>, greater<Pll>> que;
    que.push(Pll(0,0));
    while(!que.empty()){
        ll t = que.top().fi;
        ll v = que.top().se;
        que.pop();
        if(d1[v] <= t) continue;
        //if(d1[v] != 1e18) continue;
        d1[v] = t;
        for(auto& p:G[v]){
            ll c = p.fi;
            ll d = p.se.fi;
            ll u = p.se.se;
            ll rd1 = max(ll(sqrt(ld(d))-1),ll(t));
            ll rd2 = max(ll(sqrt(ld(d))),ll(t));
            ll tt1 = ll(rd1) + c + ll(d/(rd1+1));
            ll tt2 = ll(rd2) + c + ll(d/(rd2+1));
            // cout << L << " " << R << " " << r1 << " " << r2 << endl;

            que.push(Pll(min(tt1,tt2),u));
        }
    }
    if(d1[N-1] == 1e18){
        drop(-1);
    }
    cout << d1[N-1] << endl;
}
