1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<cmath>
6 #include<algorithm>
7 #include<queue>
8 #define N 50005
9 #define M 100005
10 using namespace std;
11 int n,m,zz1,a[N],b[N],zz2;
12 struct ro{
13 int from,to;
14 int next,l;
15 }road[M],road2[M];
16 void build2(int x,int y,int z){
17 zz2++;
18 road2[zz2].to=y;
19 road2[zz2].from=x;
20 road2[zz2].l=z;
21 road2[zz2].next=b[x];
22 b[x]=zz2;
23 }
24 long long sum;
25 bool rz[N],rz2[N];
26 int dfn[N],low[N],zz3,st[N],top,zz4,belong[N];
27 void tar(int x)
28 {
29 zz3++;
30 dfn[x]=low[x]=zz3;
31 rz[x]=rz2[x]=1;
32 top++;
33 st[top]=x;
34 for(int i=b[x];i>0;i=road2[i].next)
35 {
36 int y=road2[i].to;
37 if(!rz2[y])
38 {
39 tar(y);
40 low[x]=min(low[x],low[y]);
41 }
42 else if(rz[y])
43 {
44 low[x]=min(low[x],dfn[y]);
45 }
46 }
47 if(low[x]==dfn[x])
48 {
49 int v;
50 zz4++;
51 do{
52 v=st[top];
53 top--;
54 rz[v]=0;
55 belong[v]=zz4;
56 }while(dfn[v]!=low[v]);
57 }
58 }
59 void build(int x,int y,int z){
60 zz1++;
61 road[zz1].to=y;
62 road[zz1].from=x;
63 road[zz1].l=z;
64 road[zz1].next=a[x];
65 a[x]=zz1;
66 }
67 struct no{
68 int left,right;
69 int mn,mid;
70 }node[N*4];
71 int mn[N],pre[N];
72 void bui(int left,int right,int x){
73 node[x].left=left;
74 node[x].right=right;
75 if(left==right)
76 {
77 node[x].mn=mn[0];
78 return;
79 }
80 int mid=(left+right)/2;
81 node[x].mid=mid;
82 bui(left,mid,2*x);
83 bui(mid+1,right,2*x+1);
84 node[x].mn=min(node[2*x].mn,node[2*x+1].mn);
85 }
86 void change(int left,int right,int x,int z){
87 if(node[x].left==node[x].right)
88 {
89 node[x].mn=z;
90 return;
91 }
92 int mid=node[x].mid;
93 if(left>mid)change(left,right,2*x+1,z);
94 else change(left,right,2*x,z);
95 node[x].mn=min(node[x*2].mn,node[2*x+1].mn);
96 }
97 int get(int x){
98 if(node[x].left==node[x].right)
99 return node[x].left;
100 if(node[x*2].mn>node[2*x+1].mn) return get(2*x+1);
101 else return get(2*x);
102 }
103 bool fl[N];
104 int main(){
105 while(~scanf("%d%d",&n,&m)){
106 if(n==m&&n==0)break;
107 zz2=zz1=zz3=zz4=0;
108 top=0;
109 memset(fl,0,sizeof(fl));
110 memset(pre,0,sizeof(pre));
111 memset(mn,0x7f,sizeof(mn));
112 memset(belong,0,sizeof(belong));
113 memset(rz,0,sizeof(rz));
114 memset(rz2,0,sizeof(rz2));
115 memset(a,0,sizeof(a));
116 memset(b,0,sizeof(b));
117 sum=0;
118 for(int i=1;i<=m;i++)
119 {
120 int x,y,z;
121 scanf("%d%d%d",&x,&y,&z);
122 build2(x,y,z);
123 if(m==n-1)sum+=z;
124 }
125 if(m==n-1)
126 {
127 printf("%lld\n",sum);
128 continue;
129 }
130 for(int i=0;i<n;i++)
131 {
132 if(!rz2[i])tar(i);
133 }
134 for(int i=0;i<n;i++)
135 {
136 for(int j=b[i];j>0;j=road2[j].next)
137 {
138 int y=road2[j].to;
139 if(belong[i]!=belong[y])
140 {
141 build(belong[i],belong[y],road2[j].l);
142 }
143 }
144 }
145 bui(1,zz4,1);
146 mn[belong[0]]=0;
147 for(int i=1;i<=zz4;i++)
148 {
149 int bj=belong[0];
150 if(i!=1)
151 {
152 bj=get(1);
153 }
154 fl[bj]=1;
155 change(bj,bj,1,mn[0]);
156 for(int j=a[bj];j>0;j=road[j].next)
157 {
158 int y=road[j].to;
159 if(mn[y]>road[j].l)
160 {
161 pre[y]=j;
162 mn[y]=road[j].l;
163 if(!fl[y])
164 {
165 change(y,y,1,mn[y]);
166 }
167 }
168 }
169 }
170 for(int i=1;i<=zz4;i++)
171 {
172 if(i==belong[0]) continue;
173 sum+=road[pre[i]].l;
174 }
175 printf("%lld\n",sum);
176 }
177 return 0;
178 }