def root a
	return a if @p[a] == a
	return (@p[a] = root(@p[a]))
end

def isSameSet a, b
	return root(a) == root(b)
end

def unite a, b
	@p[root(a)] = root(b)
end

while true
	n,m = gets.split(" ").map{|e|e.to_i}
	break if n==0 && m==0
	cost=0
	@p=[]
	n.times{|t|@p.push t}
	info=[]
	m.times{
		a,b,c=gets.split(" ").map{|e|e.to_i}
		info.push [c,a,b]
	}
	info.sort!

	info.each{|e|
		if !isSameSet(e[2],e[1])
			unite(e[2],e[1])
			cost+=e[0]
		end
	}

	p cost
end