#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
int cost[100][100];
int mincost[100];
bool used[100];
const int INF = 1e6;
int main(void){
  int n,m;
  while(cin>>n>>m){
    if(n==0) break;
    REP(i,n){
      REP(j,n)cost[i][j] = INF;
      mincost[i]=INF;
      used[i] = false;
    }
    REP(i,m){
      int a,b,c;
      cin>>a>>b>>c;
      cost[a][b] = cost[b][a] = c;
    }
    mincost[0] = 0;
    int ans = 0;
    while(true){
      int v = -1;
      REP(u,n)
        if(!used[u]&&(v==-1||mincost[u]<mincost[v]))
          v = u;
      if(v==-1) break;
      ans += mincost[v];
      used[v] = true;
      REP(u,n)mincost[u] = min(mincost[u],cost[v][u]);
    }
    cout<<ans<<endl;
  }

  return 0;
}