Go: // Find the minimum spanning tree by the Kruskal algorithm
// -------------------------------------------------------
// Kruskal(G):
//     A = ∅
//     foreach v ∈ G.V:
//        MAKE-SET(v)
//     foreach (u, v) in G.E ordered by weight(u, v), increasing:
//         if FIND-SET(u) ≠ FIND-SET(v):
//             A = A ∪ {(u, v)}
//             UNION(u, v)
//     return A
//
// Ref: https://en.wikipedia.org/wiki/Kruskal%27s_algorithm

package main

import (
	"bufio"
	"os"
	"sort"
	"strconv"
	"strings"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)
var (
	matrix [][]int
)

type Edge struct {
	a, b, cost, added int
}
type ByCost []Edge

var edges ByCost

func (a ByCost) Len() int           { return len(a) }
func (a ByCost) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByCost) Less(i, j int) bool { return a[i].cost < a[j].cost }

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		matrix = make([][]int, n)
		for i := range matrix {
			matrix[i] = make([]int, n)
		}
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf_ := strings.Split(scanner.Text(), " ")
			a, _ := strconv.Atoi(buf_[0])
			b, _ := strconv.Atoi(buf_[1])
			cost, _ := strconv.Atoi(buf_[2])
			matrix[a][b] = cost
			matrix[b][a] = cost
		}
		sum := Kruskal(n)
		fmt.Println(sum)
		if len(edges) > 0 {
			edges = []Edge{}
		}
	}
}

func Kruskal_func(size, edge_num int) int {
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if matrix[i][j] > 0 {
				tmp := Edge{i, j, matrix[i][j], 0}
				edges = append(edges, tmp)
			}
		}
	}
	sort.Sort(ByCost(edges))
	UF_Init(edge_num)
	for i := 0; i < edge_num; i++ {
		a := edges[i].a
		b := edges[i].b
		if UF_Find(a) != UF_Find(b) {
			edges[i].added = 1
			UF_Union(UF_Find(a), UF_Find(b))
		}
	}
	sum := 0
	for i := 0; i < edge_num; i++ {
		if edges[i].added != 0 {
			// log.Printf("edge(%d,%d) is added\n", edges[i].a, edges[i].b)
			sum += edges[i].cost
		}
	}
	return sum
}

func Kruskal(size int) int {
	edge_num := 0
	for i := 0; i < size; i++ {
		for j := 0; j < size; j++ {
			if matrix[i][j] > 0 {
				edge_num++
			}
		}
	}
	return Kruskal_func(size, edge_num)
}

/*** Union-Find functions ***/
var UF_Parent []int
var UF_Rank []int

func UF_Init(n int) {
	UF_Parent = make([]int, n)
	UF_Rank = make([]int, n)
	for i := 0; i < n; i++ {
		UF_Parent[i] = i
		UF_Rank[i] = 0
	}
}
func UF_Union(a, b int) {
	if UF_Rank[a] < UF_Rank[b] {
		UF_Parent[a] = b
	} else {
		UF_Parent[b] = a
	}
	if UF_Rank[a] == UF_Rank[b] {
		UF_Rank[a]++
	}
}
func UF_Find(a int) int {
	if UF_Parent[a] == a {
		return a
	}
	return UF_Find(UF_Parent[a])
}
/*** Union-Find functions ***/

C++: 
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <memory>
#include <string>

#include <algorithm>
#include <complex>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <bitset>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
#define strtok_r strtok_s
#endif

#define ITER(c)             __typeof__((c).begin())
#define FOREACH(it, c)      for (ITER(c) it=(c).begin(); it != (c).end(); ++it)
#define RITER(c)            __typeof__((c).rbegin())
#define RFOREACH(it, c)     for (RITER(c) it=(c).rbegin(); it != (c).rend(); ++it)
#define REP(i, n)           REPEAT(i, 0, n)
#define RREP(i, n)          RREPEAT(i, 0, n)
#define REPEAT(i, k, n)     for(int i = (k); i < ((k)+(n)); ++i)
#define RREPEAT(i, k, n)    for(int i = (k)+(n)-1; i >= (k); --i)
#define FROMTO(i,f,t)       for(int i = f; i < t; i++)
#define ALL(c)              (c).begin(), (c).end()
#define LLPOW(p1,p2)        ((ll)pow((double)(p1), (int)p2))
#define ESIZEOF(A)          (sizeof(A)/sizeof((A)[0]))
#define CIN_NO_SYNC         do { cin.tie(0); ios_base::sync_with_stdio(false); } while(0)
#define GETSTR(p)           fgets((p), sizeof(p), stdin)
#define CHOMP(p)            do{ char *_q = (p) + strlen(p)-1; if(*_q == '\n' || *_q == '\r') *_q = 0; } while(0)
#define FILL(m,v)           memset(m, v, sizeof(m))
#define mp                  make_pair
#define pb                  push_back
template<class _T> _T MAX(_T p1,_T p2){return (p1>p2)?p1:p2;} 
template<class _T> _T MIN(_T p1,_T p2){return (p1<p2)?p1:p2;} 
template <class _Tv> inline string join(const _Tv &v,string sep=" "){
    ostringstream s;FOREACH(it,v){if(it!=v.begin())s<<sep;s<<*it;};return s.str();
}

typedef long long ll;
typedef unsigned long long ull;
typedef double D;
typedef complex<D> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define X real()
#define Y imag()
#define EPS (1e-9)
#define DEQ(p1,p2) (abs((p1)-(p2)) < EPS)

#ifdef _DEBUG
template<class _Tv> inline void _prfe(const char *_n,const _Tv _c,bool _p=false){
    ITER(_c) _it=_c.begin();
    if(_p){cout<<_n<<" = ["<<endl;for(;_it!=_c.end();++_it)cout<<"    "<<*_it<<endl;cout<<"]"<<endl; }
    else{cout<<_n<<" = [ ";if(_c.size()){cout<<*_it++;for(;_it!=_c.end();++_it)cout<<", "<<*_it;}cout<<" ]"<<endl; }
}
ostream &operator<<(ostream &os, const pair<int,int> &p){return(os<<"("<<p.first<<","<<p.second<<")");}
#define pf printf
#define pr(n) do{cout<<#n" = "<<(n)<<endl;}while(0)
#define prfe(n) _prfe(#n, n)
#define prfep(n) _prfe(#n, n, true)
#define dbgchk(n) do{if(n)throw;}while(0)
#else
#define pf(...) /* */
#define pr(...) /* */
#define prfe(...) /* */
#define prfep(...) /* */
#define dbgchk(...) /* */
#endif

template <class _Tn>
class UnionFind {
    int m_node;
    vector<_Tn> node;
public:
    UnionFind(int n = 0){ init(n);}
    void init(int n){ m_node = n; node = vector<_Tn>(n,-1); }
    bool connect(_Tn n1, _Tn n2){
        dbgchk(n1 < 0 || n2 < 0 || n1 >= m_node || n2 >= m_node);
        n1 = root(n1); n2 = root(n2);
        if(n1 == n2) return false;
        if(node[n1] > node[n2]) swap(n1,n2);
        node[n1] += node[n2]; node[n2] = n1;
        return true;
    }
    bool is_connect(_Tn n1, _Tn n2){ return root(n1) == root(n2); }
    _Tn root(_Tn n){
        dbgchk(n < 0 || n >= m_node);
        return (node[n] < 0) ? n : root(node[n]);
    }
    int size(_Tn n){
        idbgchkf(n < 0 || n >= m_node);
        return -node[root(n)];
    }
    void dbgprint(){
        REP(i,m_node){
            if(node[i] >= 0) continue;
            printf("(%d:%d", -node[i], i);
            REP(j,m_node){
                if(i == j) continue;
                if(root(j) == i) printf(",%d", j);
            }
            printf("),");
        }
        printf("\n");
    }
};

template <class _T1, class _T2>
class kruskal_branch {
public:
    _T1 node1, node2;
    _T2 weight;
    kruskal_branch(_T1 n1, _T1 n2, _T2 w) : node1(n1), node2(n2), weight(w) {;}
    bool operator> (const kruskal_branch<_T1, _T2> &p) const { return (weight > p.weight); }
};

template <class _T1, class _T2>
class kruskal {
public:
    typedef kruskal_branch<_T1,_T2> kruskal_branch_t;
    typedef priority_queue<kruskal_branch_t, vector<kruskal_branch_t>, greater<kruskal_branch_t> > kruskal_queue_t;
private:
    int m_nodes;
    kruskal_queue_t queue, mintree;
    UnionFind<_T1> node;
public:
    kruskal() : m_nodes(0){;}
    void nodenum_set(int n){ m_nodes = MAX(m_nodes, n); }
    void branch_add(kruskal_branch_t &b){
        dbgchk(b.node1 < 0 || b.node2 < 0);
        queue.push(b); m_nodes = MAX(m_nodes, MAX(b.node1+1, b.node2+1));
    }
    kruskal_queue_t &get_mintree(void){ return mintree; }
    UnionFind<_T1> &get_node(void){ return node; }

    _T2 solve(){
        _T2 totalcost = 0;
        node.init(m_nodes);
        for(; !queue.empty(); queue.pop()){
            const kruskal_branch_t &b = queue.top();
            if(!node.is_connect(b.node1, b.node2)){
                mintree.push(b);
                totalcost += b.weight;
                node.connect(b.node1, b.node2);
            }
        }
        return totalcost;
    }
};

typedef kruskal<int,int> kruskal_t;
typedef kruskal_t::kruskal_queue_t kruskal_queue_t;
typedef kruskal_t::kruskal_branch_t kruskal_branch_t;

int solve(int n, kruskal_t &k){
    k.solve();
    kruskal_queue_t &mintree = k.get_mintree();

    int total = 0;
    for(; !mintree.empty(); mintree.pop()){
        const kruskal_branch_t &b = mintree.top();
        pf("node1 = %d, node2 = %d, weight = %d\n", b.node1, b.node2, b.weight);
        total += b.weight;
    }
    return total;
}

int main(){
    int n, m;

    while(cin >> n >> m){
        if(n == 0 && m == 0) break;
        kruskal_t k;
        REP(i,m){
            int a, b, cost;
            cin >> a >> b >> cost;
            kruskal_branch_t branch(a, b, cost);
            k.branch_add(branch);
        }
        cout << solve(n, k) << endl;;
    }
    return 0;
}