#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define fr first
#define sc second
#define Rep(i,n) for(int i=0;i<(n);i++)
#define Rrep(i, m, n) for(int i=(m);i<(n);i++)
#define All(v) v.begin(),v.end()
#define Uniq(v) v.erase(unique(All(v)),v.end())
typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
typedef pair<string, int> Psi;
typedef vector<int> Vi;
const int INF = (1<<30);
const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};

int n, m;
int cost[101][101];
int mincost[101];
bool used[101];


int prim() {
  fill_n(mincost, 101, INF);
  fill_n(used, 101, false);

  mincost[0] = 0;
  int res = 0;

  while( 1 ) {
    int v = -1;
    Rep(u, n) {
      if( !used[u] && (v == -1 || mincost[u] < mincost[v]) ) v = u;
    }

    if( v == -1 ) break;
    used[v] = true;
    res += mincost[v];

    Rep(u, n) {
      mincost[u] = min(mincost[u], cost[v][u]);
    }
  }

  return res;
}

	  
int main()
{
  while( cin >> n >> m, n+m ) {
    fill_n(*cost, 101*101, INF);

    Rep(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      cost[a][b] = c;
      cost[b][a] = c;
    }

    cout << prim() << endl;
  }

}