#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll long long
using namespace std;

const int N = 10010;
const int M = 20010;

int n,m,e_num,ans,k;
int nxt[M*2],to[M*2],w[M*2],h[N],dis[N],fa[N],pre[N],bl[M*2],val[M*2];
bool in[N];

struct Node {
  int x,y,z,c,f;
  bool operator < (const Node &a) const {
    return f==a.f?c<a.c:f>a.f;
  }
}e[M];

queue<int> q;

int gi() {
  int x=0,o=1; char ch=getchar();
  while(ch!='-' && (ch<'0' || ch>'9')) ch=getchar();
  if(ch=='-') o=-1,ch=getchar();
  while(ch>='0' && ch<='9') x=x*10+ch-'0',ch=getchar();
  return o*x;
}

void add(int x, int y, int z, int c, int id) {
  nxt[++e_num]=h[x],to[e_num]=y,w[e_num]=z,bl[e_num]=id,val[e_num]=c,h[x]=e_num;
}

void spfa() {
  memset(dis,63,sizeof(dis));
  dis[1]=0,in[1]=1,q.push(1);
  while(!q.empty()) {
    int u=q.front();
    in[u]=0,q.pop();
    for(int i=h[u]; i; i=nxt[i]) {
      int v=to[i];
      if(dis[u]+w[i]<dis[v]) {
	pre[v]=i;
	dis[v]=dis[u]+w[i];
	if(!in[v]) in[v]=1,q.push(v);
      }
      else if(dis[u]+w[i]==dis[v]) {
	if(val[i]<val[pre[v]]) pre[v]=i;
      }
    }
  }
}

int find(int x) {
  return x==fa[x]?x:fa[x]=find(fa[x]);
}

void init() {
  e_num=ans=k=0;
  memset(h,0,sizeof(h));
  memset(pre,0,sizeof(pre));
}

int main() {
  while(scanf("%d%d", &n, &m) && n+m) {
    init();
    for(int i=1; i<=m; i++) {
      int x=gi(),y=gi(),z=gi(),c=gi();
      add(x,y,z,c,i),add(y,x,z,c,i);
      e[i]=(Node){x,y,z,c,0};
    }
    spfa();
    for(int i=2; i<=n; i++) {
      e[bl[pre[i]]].f=1;
    }
    sort(e+1,e+m+1);
    for(int i=1; i<=n; i++) fa[i]=i;
    for(int i=1; i<=m; i++) {
      int x=e[i].x,y=e[i].y;
      int xx=find(x),yy=find(y);
      if(xx==yy) continue;
      ans+=e[i].c,fa[yy]=xx,k++;
      if(k==n-1) break;
    }
    printf("%d\n", ans);
  }
  return 0;
}