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++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define MAX 101
#define INF (1<<29)

int n, m;
int cost[MAX][MAX];
int minCost[MAX];
bool used[MAX];

int prim(){
  fill(minCost, minCost+MAX, INF);
  memset(used, false, sizeof(used));
  
  minCost[0] = 0;
  int ans = 0;
  
  while(true){
    int v = -1;
    for(int i = 0 ; i < n ; i++){
      if(!used[i] && (v == -1 || minCost[i] < minCost[v])) v = i;
    }
    
    if(v == -1) break;
    used[v] = true;
    ans += minCost[v];
    
    for(int i = 0 ; i < n ; i++){
      minCost[i] = min(minCost[i], cost[v][i]);
    }
  }
  return ans;
}
    
int main(){
  while(cin >> n >> m){
    if(n == 0 && m == 0) break;
    fill(cost[0], cost[0]+MAX*MAX, INF);
    int a, b, C;
    for(int i = 0 ; i < m ; i++){
      cin >> a >> b >> C;
      cost[a][b] = C;
      cost[b][a] = C;
    }
    cout << prim() << endl;
  }
  return 0;
}

C++: #include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<numeric>
#include<sstream>
#include<iostream>
#include<algorithm>
#include<functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

const int dx[]={1,0,-1,0},dy[]={0,-1,0,1};
int n;//都市の数
int m;//橋の数
/*クラスカル法*/
const int MAX_E = 110;
const int MAX_V = 110;
int V,E;//頂点数、辺の数
struct edge{
	int u,v,cost;//頂点uからvへのcost
	edge(int _u,int _v,int _cost){u =_u;v = _v;cost =_cost;};
};
vector<edge> es;
//union-find
int parent[MAX_V];//parent[x]=xの時xが根である
int rank[MAX_V];//木の高さ
void init(int V){
	for(int i= 0;i < V;i++){
		parent[i] = i;
		rank[i] =0;
	}
}

int find(int x){
	if(parent[x] == x)
		return x;
	else{
		return find(parent[x]);
	}
}

void unit(int x,int y){
	x = find(x);
	y = find(y);
	if(x == y)
		return;
	if(rank[x] < rank[y]){
		parent[x] = y;
	}
	else{
		parent[y] = x;
		if(rank[x] == rank[y])
			rank[x]++;
	}
}

bool same(int x,int y){
	return find(x) == find(y);
}
bool comp(const edge& e1,const edge& e2){
	return e1.cost <e2.cost;
}

int kruskal(){
	sort(es.begin(),es.end(),comp);
	init(V);
	int res = 0;
	for(int i = 0;i < E;i++){
		edge e = es[i];
		if(!same(e.u,e.v)){
			unit(e.u,e.v);
			res += e.cost;
		}
	}
	return res;
}


int main(){
	while(cin>>V>>E,V||E){
		es.clear();
		rep(i,E){
			int a,b,c;
			cin>>a>>b>>c;
			es.push_back(edge(a,b,c));
		}
		cout <<kruskal()<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<P, int> P3;
const ll MOD = ll(1e9)+7;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(1e5 + 5);
const double EPS = 1e-11;
const int di[] = {0, 1, 0, -1}, dj[] = {1, 0, -1, 0};
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define ALL(v) (v).begin(), (v).end()

const int MAX_V = 100000;

struct UnionFindTree {
    int par[MAX_V];
    int rank[MAX_V];

    void init(int n) {
        for (int i = 0; i < n; i++) {
            par[i] = i;
            rank[i] = 0;
        }
    }

    int find(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;

        if (rank[x] < rank[y]) {
            par[x] = y;
        } else {
            par[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
        }
    }

    bool same(int x, int y) { return find(x) == find(y); }
};

struct edge {
    int u, v, cost;
};

bool comp(const edge &e1, const edge &e2) { return e1.cost < e2.cost; }

vector<edge> es;
int V, E;

int kruskal() {
    sort(es.begin(), es.end(), comp);
    UnionFindTree a;
    a.init(V);
    int res = 0;
    for (int i = 0; i < es.size(); i++) {
        edge e = es[i];
        if (!a.same(e.u, e.v)) {
            a.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}
int main() {
    while(cin >> V >> E, V){
        REP(i,E){
            int a, b, c;
            cin >> a >> b >> c;
            es.push_back({a,b,c});
        }
        cout << kruskal() << endl;
        es.clear();
    }
    return 0;
}