#include using namespace std; using ll = long long; #define all(a) (a).begin(), (a).end() #define pb push_back #define fi first #define se second mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count()); const ll MOD1000000007 = 1000000007; const ll MOD998244353 = 998244353; const ll MOD[3] = {999727999, 1070777777, 1000000007}; const ll LINF = 1LL << 60LL; const int IINF = (1 << 30) - 2; template struct edge{ int from; int to; T cost; int id; edge(){} edge(int to, T cost=1) : from(-1), to(to), cost(cost){} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){} void reverse(){swap(from, to);} }; template struct edges : std::vector>{ void sort(){ std::sort( (*this).begin(), (*this).end(), [](const edge& a, const edge& b){ return a.cost < b.cost; } ); } }; template struct graph : std::vector>{ private: int n = 0; int m = 0; edges es; bool dir; public: graph(int n, bool dir) : n(n), dir(dir){ (*this).resize(n); } void add_edge(int from, int to, T cost=1){ if(dir){ es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m++)); }else{ if(from > to) swap(from, to); es.push_back(edge(from, to, cost, m)); (*this)[from].push_back(edge(from, to, cost, m)); (*this)[to].push_back(edge(to, from, cost, m++)); } } int get_vnum(){ return n; } int get_enum(){ return m; } bool get_dir(){ return dir; } edge get_edge(int i){ return es[i]; } edges get_edge_set(){ return es; } }; template struct redge{ int from, to; T cap, cost; int rev; redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){} redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){} }; template using Edges = vector>; template using weighted_graph = vector>; template using tree = vector>; using unweighted_graph = vector>; template using residual_graph = vector>>; template struct mincostflow{ residual_graph G; const T TINF = std::numeric_limits::max() / 2; int n; mincostflow(residual_graph &G_){ n = (int)G_.size(); G.resize(n); for(int from=0; from e : G_[from]){ G[from].push_back(redge(e.to, e.cap, e.cost, (int)G[e.to].size())); G[e.to].pb(redge(from, 0, -e.cost, (int)G[from].size()-1)); } } } T flow(int s, int t, T f){ residual_graph H(n); vector h(n, 0); //ポテンシャル vector dist(n, 0); //最短距離 vector prevv(n, 0); // 直前の頂点 vector preve(n, 0); // 直前の辺 for(int from=0; from e : G[from]){ H[from].push_back(e); } } T res = 0; while(f > 0){ //ダイクストラ法を用いてhを更新 priority_queue, vector>, greater>> PQ; for(int i=0; i p = PQ.top(); PQ.pop(); int v = p.se; if(dist[v] < p.fi) continue; for(int i=0; i<(int)H[v].size(); i++){ redge &e = H[v][i]; if(e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){ dist[e.to] = dist[v] + e.cost + h[v] - h[e.to]; prevv[e.to] = v; preve[e.to] = i; PQ.push({dist[e.to], e.to}); } } } if(dist[t] == TINF){ //これ以上流せない return -1; } for(int v=0; v &e = H[prevv[v]][preve[v]]; e.cap -= d; H[v][e.rev].cap += d; } } return res; } }; void solve(){ int n, m; cin >> n >> m; residual_graph G(n); for(int i=0; i> u >> v; ll c, d; cin >> c >> d; u--; v--; G[u].pb(redge(v, 1, d)); G[u].pb(redge(v, 1, c)); G[v].pb(redge(u, 1, d)); G[v].pb(redge(u, 1, c)); } mincostflow mcf(G); cout << mcf.flow(0, n-1, 2) << endl; } int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int T=1; //cin >> T; while(T--) solve(); }