using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Enu = System.Linq.Enumerable;

class Program
{
    int N, M;

    void Solve()
    {
        while ((N = read.Int()) > 0)
        {
            M = read.Int();
            var edges = Enu.Range(0, N).Select(i => new List<Edge>()).ToArray();
            for (int i = 0; i < M; i++)
            {
                int a = read.Int() - 1, b = read.Int() - 1, d = read.Int(), c = read.Int();
                edges[a].Add(new Edge(b, c, d));
                edges[b].Add(new Edge(a, c, d));
            }
            Console.WriteLine(Prim(edges));
        }
    }

    int Prim(List<Edge>[] edges)
    {
        int sumCost = 0;
        bool[] seen = new bool[N];
        var heap = new Heap<Vertex>();
        heap.Push(new Vertex(0, 0, 0));

        while (heap.Count > 0)
        {
            var v = heap.Pop();
            if (seen[v.v]) continue;
            seen[v.v] = true;
            sumCost += v.cost;
            foreach (var e in edges[v.v])
            {
                if (!seen[e.to]) heap.Push(new Vertex(e.to, v.dist + e.dist, e.cost));
            }
        }

        return sumCost;
    }

    struct Vertex : IComparable<Vertex>
    {
        public int v, dist, cost;
        public Vertex(int v, int d, int c) { this.v = v; dist = d; cost = c; }
        public int CompareTo(Vertex other)
        {
            if (dist != other.dist) return dist.CompareTo(other.dist);
            return cost - other.cost;
        }
    }
    struct Edge
    {
        public int to, cost, dist;
        public Edge(int t, int c, int d) { to = t; cost = c; dist = d; }
    }

    class Heap<T>
    {
        public int Count { get { return size; } private set { size = value; } }
        private int size;
        private readonly int sign;
        private readonly List<T> A = new List<T>();
        private readonly Comparer<T> comp = Comparer<T>.Default;
        public Heap(bool greater = false)
        {
            sign = greater ? 1 : -1;
        }
        public T Peek() { return A[0]; }
        public void Push(T val)
        {
            if (size < A.Count) A[size] = val;
            else A.Add(val);
            Count++;
            int i = Count - 1;
            while (i > 0)
            {
                int parent = (i - 1) / 2;
                if (Math.Sign(comp.Compare(val, A[parent])) == sign)
                {
                    A[i] = A[parent];
                    i = parent;
                }
                else { break; }
            }
            A[i] = val;
        }
        public T Pop()
        {
            T res = A[0];
            T val = A[--Count];
            if (Count == 0) return res;
            int i = 0;
            while (i * 2 + 1 < Count)
            {
                int L = i * 2 + 1;
                int R = i * 2 + 2;
                if (R < Count && Math.Sign(comp.Compare(A[R], A[L])) == sign) { L = R; }
                if (Math.Sign(comp.Compare(A[L], val)) == sign) { A[i] = A[L]; i = L; }
                else { break; }
            }
            A[i] = val;
            return res;
        }
    }

    static void Main() { new Program().Solve(); }
    static Read read = new Read(Console.In);
    class Read
    {
        private readonly TextReader reader;
        private Queue<string> que = new Queue<string>();
        public Read(TextReader r) { reader = r; }
        public bool HasNext() { return Enqueue(); }
        public string String() { return Dequeue(); }
        public int Int() { return int.Parse(Dequeue()); }
        public long Long() { return long.Parse(Dequeue()); }
        public double Double() { return double.Parse(Dequeue()); }
        public int[] IntArray() { return Array.ConvertAll(Line().Split(), int.Parse); }
        public int[][] IntGrid(int H) { return Enumerable.Range(0, H).Select(i => IntArray()).ToArray(); }
        public string[] StringArray(int N) { return Enumerable.Range(0, N).Select(i => Line()).ToArray(); }
        public string Line() { return reader.ReadLine().Trim(); }
        private bool Enqueue()
        {
            if (que.Count > 0) return true;
            string line = Line();
            if (string.IsNullOrEmpty(line)) return false;
            foreach (var s in line.Split()) que.Enqueue(s);
            return true;
        }
        private string Dequeue() { Enqueue(); return que.Dequeue(); }
    }
}

Measured memory_bytes: 9076