#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
int main(void){
    int n, m;
    cin >> n >> m;
    vector< int > a(m), b(m), c(m), d(m);
    vector< vector< int > > edg(n+1, vector< int >());
    for(int i = 0; i < m; i++){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
        edg[a[i]].push_back(i);
        edg[b[i]].push_back(i);
    }

    vector< long long > dist(n+1, 1e18);
    using P = pair<long long, int>;
    priority_queue< P, vector<P>, greater<P> > q;
    dist[1] = 0;
    q.push(P(0, 1));
    while(!q.empty()){
        P p = q.top();
        q.pop();
        int v = p.second;
        if(dist[v] < p.first) continue;
        // cerr << v << " " << dist[v] << endl;
        for(int i: edg[v]){
            int to = b[i];
            if(to == v) to = a[i];
            long long cost = c[i] + (d[i] / (dist[v]+1));
            if(dist[v] + cost < dist[to]){
                dist[to] = dist[v] + cost;
                q.push(P(dist[to], to));
            }
            cost = c[i] + (d[i] / (dist[v]+2)) + 1;
            if(dist[v] + cost < dist[to]){
                dist[to] = dist[v] + cost;
                q.push(P(dist[to], to));
            }
            int rtd = sqrt(d[i]) - dist[v];
            for(int j = -10; j <= 10; j++){
                int w = rtd + j;
                if(w <= 0) continue;
                cost = c[i] + (d[i] / (dist[v]+1+w)) + w;
                if(dist[v] + cost < dist[to]){
                    dist[to] = dist[v] + cost;
                    q.push(P(dist[to], to));
                }
            }
        }
    }
    if(dist[n] == 1e18){
        cout << -1 << endl;
    }else{
        cout << dist[n] << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T1, class T2> inline bool chmax(T1 &a, const T2 &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T1, class T2> inline bool chmin(T1 &a, const T2 &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T> void verr(const set<T> &st) { repa(a, st) cerr << a << " "; cerr << endl; }
template <class S, class T> void verr(const map<S, T> &mp) { repa(a, mp) cerr << "{" << a.first << ", " << a.second << "} "; cerr << endl; }
template <class S, class T, class N> void verr(const vector<pair<S,T>>& a, const N& n) { rep(i, n) cerr << "{" << a[i].first << ", " << a[i].second << "} "; cerr << endl; }
template <class T, class N> void verr(const vector<T>& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << endl; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << endl; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
vector<ll> topologycal_sort(vector<vector<ll>> L) {
    ll N = L.size();
    vector<ll> degree(N, 0), sorted_array;
    rep(i, N) for (auto v : L[i]) degree[v]++;

    queue<ll> que;
    rep(i, N) if (degree[i] == 0) que.emplace(i);

    while (!que.empty()) {
        ll u = que.front();
        que.pop();
        for (auto v : L[u])
            if (--degree[v] == 0) que.emplace(v);
        sorted_array.emplace_back(u);
    }
    // 各ノード番号をトポロジカル順にしたリスト
    return sorted_array;
}

int main() {
    ll N, M;
    cin >> N >> M;
    vl A(N);
    rep(i, N) cin >> A[i];

    vvc<ll> G(N);
    rep(i, M){
        ll a, b;
        cin >> a >> b, a--, b--;
        G[a].eb(b);
    }

    vl T = topologycal_sort(G);

    ll ans = -INF;
    vl B(N, INF), S(N, -INF);
    rep(i, N){
        ll now = T[i];
        chmax(ans, A[now] - B[now]);
        repa(to, G[now]){
            chmin(B[to], A[now]);
            chmin(B[to], B[now]);
        }
    }
    cout << ans << endl;
}