#include #include using namespace std; typedef unsigned long long _ulong; typedef long long int lint; typedef pair plint; typedef pair pld; #define ALL(x) (x).begin(), (x).end() #define SZ(x) ((lint)(x).size()) #define FOR(i, begin, end) for(lint i=(begin),i##_end_=(end);i=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define endk '\n' template auto add = [](T a, T b) -> T { return a + b; }; template auto f_max = [](T a, T b) -> T { return max(a, b); }; template auto f_min = [](T a, T b) -> T { return min(a, b); }; template using V = vector; using Vl = V; using VVl = V; template< typename T > ostream& operator<<(ostream & os, const vector< T > & v) { for (int i = 0; i < (int)v.size(); i++) os << v[i] << (i + 1 != v.size() ? " " : ""); return os; } template< typename T >istream& operator>>(istream & is, vector< T > & v) { for (T& in : v) is >> in; return is; } template bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } lint gcd(lint a, lint b) { if (b == 0) return a; else return gcd(b, a % b); } lint ceil(lint a, lint b) { return (a + b - 1) / b; } lint digit(lint a) { return (lint)log10(a); } lint dist(plint a, plint b) { return abs(a.first - b.first) * abs(a.first - b.first) + abs(a.second - b.second) * abs(a.second - b.second); } void Worshall_Floyd(VVl& g) { REP(k, SZ(g)) REP(i, SZ(g)) REP(j, SZ(g)) chmin(g[i][j], g[i][k] + g[k][j]); } const lint MOD = 998244353, INF = 5e18; lint dx[8] = { 1, 0, -1, 0, 1, -1, 1, -1 }, dy[8] = { 0, 1, 0, -1, -1, -1, 1, 1 }; void YN(bool flag) { cout << (flag ? "YES" : "NO") << endk; } void yn(bool flag) { cout << (flag ? "Yes" : "No") << endk; } typedef pair Pa; typedef pair tlint; struct Edge { lint from, to; lint cost; Edge(lint u, lint v, lint c) { cost = c; from = u; to = v; } bool operator<(const Edge& e) const { return cost < e.cost; } }; struct WeightedEdge { lint to; lint cost; WeightedEdge(lint v, lint c = 1) { to = v; cost = c; } bool operator<(const WeightedEdge& e) const { return cost < e.cost; } }; using WeightedGraph = V>; template struct Flow { const cost_t INF; struct edge { lint to; flow_t cap; cost_t cost; lint rev; }; vector > Graph; vector potential, min_cost; vector prevv, preve; vector level; vector iter; Flow(lint V) :Graph(V), INF(numeric_limits< cost_t >::max()) {} void add_edge(lint from, lint to, flow_t cap, cost_t cost = 0) { Graph[from].push_back({ to, cap, cost, SZ(Graph[to]) }); Graph[to].push_back({ from, 0, -cost, SZ(Graph[from]) - 1 }); } void bfs(lint s) { lint V = SZ(Graph); level.assign(V, -1); queue que; que.push(s); level[s] = 0; while (!que.empty()) { lint v = que.front(); que.pop(); REP(i, SZ(Graph[v])) { edge& e = Graph[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } flow_t dfs(lint v, lint t, flow_t f) { if (v == t) return f; for (lint& i = iter[v]; i < SZ(Graph[v]); i++) { edge& e = Graph[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { flow_t d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; Graph[e.to][e.rev].cap += d; return d; } } } return 0; } flow_t max_flow(lint s, lint t) { flow_t flow = 0; lint V = SZ(Graph); for (;;) { bfs(s); if (level[t] < 0) return flow; iter.assign(V, 0); flow_t f; while ((f = dfs(s, t, INF)) > 0) { flow += f; } } } lint min_cost_flow(lint s, lint t, lint f) { cost_t res = 0; lint V = SZ(Graph); potential.assign(V, 0); prevv.assign(V, -1); preve.assign(V, -1); while (f > 0) { priority_queue, vector >, greater > > que; min_cost.assign(V, INF); min_cost[s] = 0; que.push({ 0, s }); while (!que.empty()) { pair p = que.top(); que.pop(); lint v = p.second; if (min_cost[v] < p.first) continue; REP(i, SZ(Graph[v])) { edge& e = Graph[v][i]; cost_t nextCost = min_cost[v] + e.cost + potential[v] - potential[e.to]; if (e.cap > 0 && min_cost[e.to] > nextCost) { min_cost[e.to] = nextCost; prevv[e.to] = v; preve[e.to] = i; que.push({ min_cost[e.to], e.to }); } } } if (min_cost[t] == INF) { return -1; } REP(v, V) potential[v] += min_cost[v]; flow_t addflow = f; for (lint v = t; v != s; v = prevv[v]) { addflow = min(addflow, Graph[prevv[v]][preve[v]].cap); } f -= addflow; res += addflow * potential[t]; for (lint v = t; v != s; v = prevv[v]) { edge& e = Graph[prevv[v]][preve[v]]; e.cap -= addflow; Graph[v][e.rev].cap += addflow; } } return res; } }; lint N, M, u, v, s, t; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> N >> M; Flow g(N); REP(i, M) { cin >> u >> v >> s >> t; u--; v--; g.add_edge(u, v, 1, s); g.add_edge(u, v, 1, t); } cout << g.min_cost_flow(0, N - 1, 2) << endk; }