#include using namespace std; #define F first #define S second #define R cin>> #define ll long long #define ln cout<<'\n' #define in(a) insert(a) #define pb(a) push_back(a) #define pd(a) printf("%.10f\n",a) #define mem(a) memset(a,0,sizeof(a)) #define all(c) (c).begin(),(c).end() #define iter(c) __typeof((c).begin()) #define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--) #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++) ll check(ll n,ll m,ll x,ll y){return x>=0&&x=0&&yvoid pr(const A &a,const B&...b){cout<void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")< P; const int MAX_V= 100002; struct edge{ ll to,cap,cost,rev; edge(ll to,ll cap,ll cost,ll rev) : to(to),cap(cap),cost(cost),rev(rev) {} }; vector G[MAX_V]; ll dist[MAX_V]; ll prevv[MAX_V],preve[MAX_V]; void add_edge(ll from,ll 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(ll s,ll t,ll f){ ll res = 0; while(f>0){ fill(dist,dist+MAX_V,MAXL); dist[s] = 0; bool update=true; while(update){ update=false; for(ll v=0; v0 && dist[e.to]>dist[v]+e.cost){ dist[e.to]=dist[v]+e.cost; prevv[e.to]=v; preve[e.to]=i; update=true; } } } } if(dist[t]==MAXL) return -1; ll d = f; for(ll v=t; v!=s; v=prevv[v]){ d=min(d,G[prevv[v]][preve[v]].cap); } f-=d;res+=d*dist[t]; for(ll 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; } void Main() { ll n,m; cin >> n >> m; rep(i,m) { ll x,y,z1,z2; cin >> x >> y >> z1 >> z2; x--,y--; add_edge(x,y,1,z1); add_edge(y,x,1,z1); add_edge(x,y,1,z2); add_edge(y,x,1,z2); } pr(min_cost_flow(0,n-1,2)); } int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}