from networkx import* N, M = map(int, input().split()) G = MultiDiGraph() G.add_nodes_from([i for i in range(1, N - 1)]) G.add_node(0, demand = -2) G.add_node(N - 1, demand = 2) for i in range(M): x, y, c, d = map(int, input().split()) x, y = x-1, y-1 G.add_weighted_edges_from([(x, y, c), (x, y, d)], capacity = 1) print(min_cost_flow_cost(G))