#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

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

using namespace std;
typedef long long ll;

typedef pair<ll, int> P;

const ll INF = 1e18;

struct edge{
    int to;
    ll cap, cost;
    int rev;
};

/**
 * 負のコストの辺がない場合のみ使える 
 */
class MinCostFlow{
    public:
        int V;  // 頂点数
        vector<vector<edge>> G;
        MinCostFlow(int v){
            V = v;
            G = vector<vector<edge>>(v, vector<edge>());
            dist = vector<ll>(v);
            preve = vector<int>(v);
            prevv = vector<int>(v);
        }
        void add_edge(int from, int to, ll cap, ll cost){
            G[from].push_back((edge){to, cap, cost, G[to].size()});
            G[to].push_back((edge){from, 0, -cost, G[from].size()-1});
        }

        ll min_cost_flow(int s, int t, int f){
            ll res = 0;
            while(f > 0){
                fill(dist.begin(), dist.end(), INF);
                dist[s] = 0;
                priority_queue<P, vector<P>, greater<P>> que;
                que.push(P(0, s));
                while(!que.empty()){
                    P top = que.top(); que.pop();
                    int v = top.second;
                    ll d = top.first;
                    if(dist[v] != d) continue;
                    for(int i = 0; i < G[v].size(); i++){
                        edge &e = G[v][i];
                        if(e.cap > 0 && dist[e.to] > dist[v]+e.cost){
                            dist[e.to] = dist[v]+e.cost;
                            prevv[e.to] = v;
                            preve[e.to] = i;
                            que.push(P(dist[e.to], e.to));
                        }
                    }
                }
                
                if(dist[t] == INF){
                    return -1;
                }
                ll d = f;
                for(int v = t; v != s; v = prevv[v]){
                    d = min(d, G[prevv[v]][preve[v]].cap);
                }
                f -= d;
                res += d*dist[t];
                for(int v = t; v != s; v = prevv[v]){
                    edge &e = G[prevv[v]][preve[v]];
                    e.cap -= d;
                    G[v][e.rev].cap += d;
                }
            }
            return res;
        }

    private:
        vector<ll> dist;
        vector<int> prevv, preve;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m;
    cin >> n >> m;
    MinCostFlow mcf(n);
    for(int i = 0; i < m; i++){
        int u, v, c, d;
        cin >> u >> v >> c >> d;
        u--; v--;
        mcf.add_edge(u, v, 1, c);
        mcf.add_edge(u, v, 1, d);
    }
    cout << mcf.min_cost_flow(0, n-1, 2) << endl;
}