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; private set; }
        private readonly int sign;
        private readonly T[] a;
        private readonly Comparer<T> comp = Comparer<T>.Default;
        public Heap(int MaxSize = (int)5e6, bool greater = false)
        {
            sign = greater ? 1 : -1;
            a = new T[MaxSize];
        }
        public T Peek() { return a[0]; }
        public void Push(T val)
        {
            int i = Count++;
            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];
            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: 132336