using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MinimumCostFlow
{
    class Edge : IComparable
    {
        internal int from, to, cap, cost;

        public Edge(int from, int to)
        {
            this.from = from;
            this.to = to;
        }

        public Edge(int u, int v, int c, int d)
        {
            from = u;
            to = v;
            cap = c;
            cost = d;
        }

        public int CompareTo(object obj)
        {
            Edge e = obj as Edge;
            return e.cost - cost;
        }
    }

    class MinimumCostFlow
    {
        readonly int n, INFTY = int.MaxValue / 2;

        Dictionary<int, List<Edge>> G;

        public MinimumCostFlow(int V)
        {
            n = V;

            G = new Dictionary<int, List<Edge>>();
            for (int i = 0; i < n; i++) G.Add(i, new List<Edge>());
        }

        public void AddEdge(int from, int to, int cap, int cost)
        {
            G[from].Add(new Edge(from, to, cap, cost));
            G[to].Add(new Edge(to, from, 0, -cost));
        }

        public int Solve(int s, int t, int F)
        {
            int[,] capacity = new int[n, n];
            int[,] cost = new int[n, n];
            int[,] flow = new int[n, n];

            for (int i = 0; i < n; i++)
            {
                foreach (var e in G[i])
                {
                    int u = e.from;
                    int v = e.to;

                    capacity[u, v] = e.cap;
                    cost[u, v] = e.cost;
                }
            }

            int ret = 0;
            int[] h = new int[n];

            while (F > 0)
            {
                int[] d = Enumerable.Repeat(INFTY, n).ToArray();
                int[] p = Enumerable.Repeat(-1, n).ToArray();

                d[s] = 0;

                PriorityQueue<Edge> que = new PriorityQueue<Edge>();    // e < f <=> e.cost > f.cost
                que.Enqueue(new Edge(-2, s));

                while (que.Count > 0)
                {
                    Edge e = que.Dequeue();

                    if (p[e.to] != -1) continue;

                    p[e.to] = e.from;

                    foreach (var u in G[e.to])
                    {
                        if (capacity[u.from, u.to] - flow[u.from, u.to] > 0)
                        {
                            if (d[u.to] > d[u.from] + cost[u.from, u.to] + h[u.from] - h[u.to])
                            {
                                d[u.to] = d[u.from] + cost[u.from, u.to] + h[u.from] - h[u.to];
                                que.Enqueue(new Edge(u.from, u.to, 0, d[u.to]));
                            }
                        }
                    }
                }

                if (p[t] == -1) return -1;

                int f = F;

                for (int u = t; u != s; u = p[u])
                {
                    f = Math.Min(f, capacity[p[u], u] - flow[p[u], u]);
                }

                for (int u = t; u != s; u = p[u])
                {
                    ret += f * cost[p[u], u];
                    flow[p[u], u] += f;
                    flow[u, p[u]] -= f;
                }

                F -= f;

                for (int u = 0; u < n; u++) h[u] += d[u];
            }
            return ret;
        }
    }

    class PriorityQueue<T> where T : IComparable
    {
        List<T> heap;
        int size;

        public PriorityQueue() { heap = new List<T>(); }

        public void Enqueue(T x)
        {
            heap.Add(x);
            int i = size++;

            while (i > 0)
            {
                int p = (i - 1) / 2;

                if (Compare(heap[p], x) <= 0) break;

                heap[i] = heap[p];
                i = p;
            }
            heap[i] = x;
        }

        public T Dequeue()
        {
            T ret = heap[0];
            T x = heap[--size];

            int i = 0;
            while (i * 2 + 1 < size)
            {
                int a = i * 2 + 1;
                int b = i * 2 + 2;

                if (b < size && Compare(heap[b], heap[a]) < 0) a = b;
                if (Compare(heap[a], x) >= 0) break;

                heap[i] = heap[a];
                i = a;
            }
            heap[i] = x;
            heap.RemoveAt(size);
            return ret;
        }

        public int Count { get { return size; } }

        private int Compare(T x, T y)
        {
            return y.CompareTo(x);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            int V = int.Parse(input[0]);
            int E = int.Parse(input[1]);
            int F = int.Parse(input[2]);

            MinimumCostFlow MCF = new MinimumCostFlow(V);

            for (int i = 0; i < E; i++)
            {
                input = Console.ReadLine().Split(' ');
                int u = int.Parse(input[0]);
                int v = int.Parse(input[1]);
                int c = int.Parse(input[2]);
                int d = int.Parse(input[3]);

                MCF.AddEdge(u, v, c, d);
            }

            Console.WriteLine(MCF.Solve(0, V - 1, F));
        }
    }
}

Measured memory_bytes: 10464