#include using namespace std; #define fi first #define se second #define pb push_back using vi = vector ; using ll = long long; using pii = pair ; const ll mod = 998244353; //~ const ll mod = 1e9 + 7; ll qpow(ll a, ll b, ll m = mod) { ll r = 1, t = a; for(; b; b /= 2) { if(b & 1) r = r * t % m; t = t * t % m; } return r; } const int N = 1e5 + 11; const int M = N * 8; struct edge { int u, v, f; ll c; }e[M]; int ecnt = 0; int fe[N], ne[M]; void add(int u, int v, int f, ll c) { e[ecnt] = {u, v, f, c}; ne[ecnt] = fe[u]; fe[u] = ecnt ++; e[ecnt] = {v, u, 0, -c}; ne[ecnt] = fe[v]; fe[v] = ecnt ++; } ll dis[N]; bool inq[N]; const ll inf = 1e15; int p[N]; int main() { ios :: sync_with_stdio(false); int n, m; cin >> n >> m; memset(fe, -1, sizeof fe); memset(ne, -1, sizeof ne); for(int i = 1; i <= m; i ++) { int u, v; ll c, d; cin >> u >> v >> c >> d; add(u, v, 1, c); add(v, u, 1, c); add(u, v, 1, d); add(v, u, 1, d); } ll ans = 0; for(int t : {0, 1}) { deque q, rq; auto push = [&](int x) { q.push_back(x); rq.push_front(x); }; auto pop = [&]() { q.pop_front(); rq.pop_back(); }; fill(dis, dis + n + 1, inf); dis[1] = 0; inq[1] = 1; push(1); while(!q.empty()) { int x = q.front(); pop(); inq[x] = 0; for(int i = fe[x]; i != -1; i = ne[i]) if(e[i].f) { int v = e[i].v, w = e[i].c; if(dis[x] + w < dis[v]) { dis[v] = dis[x] + w; p[v] = i; if(!inq[v]) { push(v); if(dis[q[0]] > dis[q.back()]) q.swap(rq); inq[v] = 1; } } } } p[1] = -1; for(int i = p[n]; i != -1; i = p[e[i].u]) { e[i].f -= 1; e[i ^ 1].f += 1; } ans += dis[n]; } cout << ans << '\n'; }