#include <iostream>
#include <algorithm>
using namespace std;

const int INF = 1000000;
int main(){
	int n,m;
	while(cin >> n >> m){
		if(!n && !m)
			break;
		int bridge[100][100];
		int a,b,cost;
		for(int i=0;i<100;i++){
			for(int j=0;j<100;j++){
				if(i==j)
					bridge[i][j] = 0;
				else
					bridge[i][j] = INF;
			}
		}
		for(int i=0;i<m;i++){
			cin >> a >> b >> cost;
			if(bridge[a][b] > cost){
				bridge[a][b] = cost;
				bridge[b][a] = cost;
			}
		}
		
		//ツフツδ債イツド
		for(int k=0;k<n;k++){
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					int dis = bridge[i][k]+bridge[k][j];
					if(dis < bridge[i][j]){
						bridge[i][j] = dis;
					}	
				}
			}
		}

		
		int min[10000];
		int pos = 0;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(i<j){
					min[pos++] = bridge[i][j];
				}
			}
		}
		sort(min,min+pos-1);
		int sum = 0;
		for(int i=0;i<n-1;i++){
			sum+=min[i];
		}
		cout << sum << endl;
	}
	return 0;
}