#include<algorithm>
#include<cassert>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << endl

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-8;

const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

struct edge{
    int to;
    lint cost1, cost2;
};

struct node{
    int f, s;
    lint cost;

    bool operator<(const node& rhs) const {
        return cost > rhs.cost;
    }
};

template<typename T>
bool chmax(T& a, T b, bool equal = false){
    if(a < b || equal && a == b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmin(T& a, T b, bool equal = false){
    if(b < a || equal && a == b){
        a = b;
        return true;
    }
    return false;
}

bool topological_sort(const vector< vector<edge> >& g, vector<int>& res){
    const int v = (int)g.size();
    res.clear();

    vector<int> deg(v, 0);
    for(int i = 0 ; i < v ; ++i){
        for(auto e : g[i]){
            ++deg[e.to];
        }
    }

    vector<int> st;
    for(int i = 0 ; i < v ; ++i){
        if(deg[i] == 0){
            st.emplace_back(i);
        }
    }

    while(!st.empty()){
        int k = st.back();
        st.pop_back();
        res.emplace_back(k);
        for(auto e : g[k]){
            --deg[e.to];
            if(deg[e.to] == 0){
                st.emplace_back(e.to);
            }
        }
    }

    return (int)res.size() == v;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n, p;
    cin >> n >> p;

    vector< vector<edge> > g(n);
    rep(i, p){
        int s, e;
        lint t1, t2;
        cin >> s >> e >> t1 >> t2;
        g[--s].push_back({--e, t1, t2});
    }

    vector<int> v;
    assert(topological_sort(g, v));

    vector<int> rev(n);
    rep(i, n){
        rev[v[i]] = i;
    }

    vector< vector<lint> > cost(n, vector<lint>(n, LLINF));
    cost[0][0] = 0;
    priority_queue<node> que;
    que.push({0, 0, 0LL});

    while(!que.empty()){
        node now = que.top();
        que.pop();
        if(cost[now.f][now.s] < now.cost){
            continue;
        }
        if(now.f == now.s){
            for(auto e1 : g[now.f]){
                for(auto e2 : g[now.s]){
                    if(e1.to == e2.to){
                        if(chmin(cost[e1.to][e1.to], now.cost + e1.cost1 + e1.cost2)){
                            que.push({e1.to, e1.to, now.cost + e1.cost1 + e1.cost2});
                        }
                    }else{
                        if(chmin(cost[e1.to][e2.to], now.cost + e1.cost1 + e2.cost1)){
                            que.push({e1.to, e2.to, now.cost + e1.cost1 + e2.cost1});
                        }
                    }
                }
            }
        }else if(rev[now.f] < rev[now.s]){
            for(auto e : g[now.f]){
                if(chmin(cost[e.to][now.s], now.cost + e.cost1)){
                    que.push({e.to, now.s, now.cost + e.cost1});
                }
            }
        }else{
            for(auto e : g[now.s]){
                if(chmin(cost[now.f][e.to], now.cost + e.cost1)){
                    que.push({now.f, e.to, now.cost + e.cost1});
                }
            }
        }
    }

    cout << cost[n - 1][n - 1] << endl;

    return 0;
}


Measured memory_bytes: 27172