#include <iostream>
#include <vector>
#include <utility>
#include <set>
#include <algorithm>
#include <utility>

using namespace std;

int map[101][100];
int n,m;

bool check(int x,vector<bool> &used){

    used[x] = true;
    bool flg = true;
    for(int i=0;i<used.size();i++){
	if(!used[i]) flg = false;
    }
    if(flg) return true;

    bool res = false;
    for(int i=0;i<n;i++){
	if(map[x][i] > 0  && !used[i]){
	    res |= check(i,used);
	}
    }

    return res;

}

int main(){

    while(true){
    cin >> n >> m;
    if(n == 0 && m == 0) break;
    vector<int> a(m);
    vector<int> b(m);
    vector<int> cost(m);
    vector< pair <  pair<int,int> , pair<int,int> > >  q(m);
    long long int sum = 0;
    for(int i=0;i<m;i++){
	cin >> a[i] >> b[i] >> cost[i];
	sum += cost[i];
	q[i] = pair< pair<int,int>, pair<int,int> >(pair<int,int>(cost[i],a[i]), pair<int,int>(b[i],i));
	map[a[i]][b[i]] = cost[i];
	map[b[i]][a[i]] = cost[i];
    }
    sort(q.rbegin(),q.rend());
    for(int i=0;i<m;i++){
	int x = q[i].first.second ;
	int y = q[i].second.first ;
	int temp = map[x][y];
	map[x][y]  = 0;
	map[y][x]  = 0;
	vector<bool> used1(n,false);
	vector<bool> used2(n,false);
	if(check(x,used1) && check(y,used2)){
	    sum -= temp;
	}else{
	    map[x][y]  = temp;
	    map[y][x]  = temp;
	}
    }
    cout << sum << endl;
    }

    return 0;
}