#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[101][101];
		int pre_vertex[101][101];
		int a,b,cost;
		for(int i=0;i<101;i++){
			for(int j=0;j<101;j++){
				if(i==j)
					bridge[i][j] = 0;
				else
					bridge[i][j] = INF;
				pre_vertex[i][j] = i;
			}
		}
		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;
						pre_vertex[i][j] = pre_vertex[k][j];
					}	
				}
			}
		}
		int min = INF;
		for(int i=0;i<n;i++){
			int sum = 0;
			for(int j=0;j<n;j++){
				sum += bridge[i][j];
				sum -= bridge[i][pre_vertex[i][j]];
			}
			if(sum < min)
				min = sum;
		}
		cout << min << endl;
	}
	return 0;
}