#include #include #include #include #include #include #include #include #include #include #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template void chmin(T &a,const T &b){if(a>b) a=b;} template void chmax(T &a,const T &b){if(a struct MinCostFlow { const cost_t INF; struct edge { int to; flow_t cap; cost_t cost; int rev; bool isrev; }; vector> graph; vector potential,min_cost; vector prevv,preve; MinCostFlow(int V) : graph(V),INF(numeric_limits< cost_t >::max()) {} void add_edge(int from,int to,flow_t cap,cost_t cost){ graph[from].emplace_back((edge){to,cap,cost,(int)graph[to].size(),false}); graph[to].emplace_back((edge){from,0,-cost,(int)graph[from].size()-1,true}); } cost_t min_cost_flow(int s,int t,flow_t f){ int V=(int)graph.size(); cost_t ret=0; using Pi = pair; priority_queue,greater> PQ; potential.assign(V,0); preve.assign(V,-1); prevv.assign(V,-1); while(f>0){ min_cost.assign(V,INF); PQ.emplace(0,s); min_cost[s]=0; while(!PQ.empty()){ cost_t d; int now; tie(d,now)=PQ.top(); PQ.pop(); if(min_cost[now]0&&min_cost[e.to]>nextCost){ min_cost[e.to]=nextCost; prevv[e.to]=now; preve[e.to]=i; PQ.emplace(min_cost[e.to],e.to); } } } if(min_cost[t]==INF) return -1; for(int v=0;v>N>>M; MinCostFlow mcf(N); rep(i,M){ int a,b,c,d; cin>>a>>b>>c>>d; a--;b--; mcf.add_edge(a,b,1,c); mcf.add_edge(b,a,1,c); mcf.add_edge(a,b,1,d); mcf.add_edge(b,a,1,d); } ll ans=mcf.min_cost_flow(0,N-1,2); cout<