(N,M),*TC = $<.map{|ln| ln.split.map(&:to_i) }
E = [*0...N+M].combination(2).map{|i,j|
x0,y0,r0 = TC[i]
x1,y1,r1 = TC[j]
d = Math.hypot(x1-x0,y1-y0).round 12
d = if ! r0 && ! r1 then d
elsif r0 && ! r1 then (d-r0).abs
elsif ! r0 && r1 then (d-r1).abs
elsif r0+r1<d then d-r0-r1
elsif d<(r1-r0).abs then (r1-r0).abs-d
else 0
end
next i,j,d
}.sort_by{_3}
F = lambda{|g,i|
g[i]<0 ? i : g[i] = F[g,g[i]]
}
U = lambda{|g,i,j,d|
i,j = F[g,i],F[g,j]
next 0 if i==j
i,j = j,i if g[j]<g[i]
g[i] += g[j]
g[j] = i
next d
}
p (1<<M).times.map{|c|
tc = (c+1<<N)-1
g = [-1]*(N+M)
next E.sum{|i,j,d|
tc[i]+tc[j]<2 ? 0 : U[g,i,j,d]
}#.tap{warn'%0*b %.1f'%[N+M,tc,_1]}
}.min