#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
int matrix[105][105];
bool vis[105];
int dis[105];
int n,r,ans,Min,tmp;
void prim()
{
for(int i = 0; i<n; ++i)
dis[i] = matrix[0][i];
vis[0] = 1;
for(int i = 1; i<n; ++i)
{
Min = INF;
for(int j = 1; j<n; ++j)
{
if(!vis[j]&&Min>dis[j])
{
Min = dis[j];
tmp = j;
}
}
vis[tmp] = 1;
for(int j = 0; j<n; ++j)
{
if(!vis[j]&&dis[j]>matrix[tmp][j])
dis[j] = matrix[tmp][j];
}
}
for(int i = 0; i<n; ++i)
ans += dis[i];
printf("%d\n",ans);
}
int main()
{
int v1,v2,val;
while(scanf("%d",&n),n)
{
scanf("%d",&r);
ans = 0;
memset(vis,0,sizeof(vis));
for(int i = 0; i<n; ++i)
for(int j = 0; j<n; ++j)
matrix[i][j] = (i==j)?0:INF;
while(r--)
{
scanf("%d%d%d",&v1,&v2,&val);
matrix[v1-1][v2-1] = matrix[v2-1][v1-1] = min(val,min(matrix[v1-1][v2-1],matrix[v2-1][v1-1]));
}
prim();
}
return 0;
}